Is there a way to have the UIActivityIndicatorView display in the center of the screen to mirror the NetworkActivityIndicator? So I want the same process as the NetworkAc开发者_JAVA技巧tivityIndicator, just with a larger indicator that will be more noticeable to the user.
Try this:
UIView *view = /* whatever your main view is */
UIActivityIndicatorView *spinner = [[UIActivityIndicatorVIew alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect frame = spinner.frame;
frame.origin.x = view.frame.size.width / 2 - frame.size.width / 2;
frame.origin.y = view.frame.size.height / 2 - frame.size.height / 2;
spinner.frame = frame;
[view addSubview:spinner];
[spinner startAnimating];
[spinner release]; // Or, you could keep a handle to this for later to stop animating it
This basically centers the spinner in its enclosing view, whatever that might be.
If you do not keep a handle to spinner, and if spinner is the only subview of view (or at least in a well known position), you could later get a handle to it by dipping into [view subviews]
for it.
EDIT: You could also probably use CGRectGetMidX()
and CGRectGetMidY()
instead of the more complicated equations above for finding x
and y
... Your mileage may vary. :-)
EDIT (2/25/2016)
This answer has received some up-votes recently, so I thought an update was in order. There is actually an even better way to do this now. This example places a UIActivityIndicatorView
in the center of a UITableView
.
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_spinner.translatesAutoresizingMaskIntoConstraints = NO;
_spinner.hidesWhenStopped = YES;
[self.tableView addSubview:self.spinner];
[self.tableView addConstraints:@[
[NSLayoutConstraint constraintWithItem:_spinner
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.tableView
attribute:NSLayoutAttributeCenterX
multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:_spinner
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.tableView
attribute:NSLayoutAttributeCenterY
multiplier:1 constant:0]
]];
This works for me inside a UIViewController:
override func viewDidLayoutSubviews() {
activityIndicator.center = view.center
}
indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
indicatorView.center = self.view.center;
activityInd.center = CGPointMake(160,240);
-(void)setCenter:(UIView*)view{
UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[view addSubview:actView];
[actView startAnimating];
CGSize boundsSize = view.bounds.size;
CGRect frameToCenter = actView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
actView.frame = frameToCenter;
}
In Swift:
var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
override func viewDidLoad() {
activityIndicator.hidesWhenStopped = true;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray;
activityIndicator.center = view.center;
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
For more information refer the below tutorial:
http://sourcefreeze.com/uiactivityindicatorview-example-using-swift-in-ios/
This worked for me. Notice I use bounds rather than frame. The frame properly doesn't always show the rect based on the orientation (this method goes inside the view controller).
- (void) viewDidLayoutSubviews
{
if (self.spinner)
{
CGRect screenRect = self.view.bounds;
self.spinner.frame = CGRectMake(roundf((screenRect.size.width - 50) * 0.5f),
roundf((screenRect.size.height - 50) * 0.5f),
50,
50);
}
}
Only this work perfect for me, simple and fast:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[self.view addSubview:spinner];
[spinner startAnimating];
in swift 2.0 xcode 7:
override func viewDidLayoutSubviews() {
self.Loader.center = self.view.center
}
精彩评论