有没有办法让UIActivityIndicatorView显示在屏幕中央以反映NetworkActivityIndicator?所以我想要与NetworkActivityIndicator相同的流程,只需要一个更大的指标,让用户更加注意。
答案 0 :(得分:27)
试试这个:
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
这基本上将微调器放在其封闭视图中,无论它是什么。
如果你没有设置微调器的句柄,并且如果微调器是视图的唯一子视图(或者至少在一个众所周知的位置),你可以稍后通过浸入[view subviews]
来获取它的句柄。它
编辑:您也可以使用CGRectGetMidX()
和CGRectGetMidY()
代替上面更复杂的公式来查找x
和y
...您的里程可能会有所不同。 : - )
编辑(2016年2月25日)
这个答案最近收到了一些上涨票,所以我认为更新是有序的。实际上有一种更好的方法可以做到这一点。此示例将UIActivityIndicatorView
放在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]
]];
答案 1 :(得分:22)
这在UIViewController中适用于我:
- (void) viewDidLayoutSubviews {
self.activityIndicator.center = self.view.center;
}
答案 2 :(得分:9)
indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
indicatorView.center = self.view.center;
答案 3 :(得分:3)
activityInd.center = CGPointMake(160,240);
答案 4 :(得分:1)
-(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;
}
答案 5 :(得分:1)
在斯威夫特:
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.
}
有关更多信息,请参阅以下教程:
http://sourcefreeze.com/uiactivityindicatorview-example-using-swift-in-ios/
答案 6 :(得分:0)
这对我有用。注意我使用的是边界而不是框架。框架不能总是根据方向显示矩形(此方法进入视图控制器内)。
- (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);
}
}
答案 7 :(得分:0)
只有这项工作对我来说非常完美,简单快捷:
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];
答案 8 :(得分:0)
在swift 2.0 xcode 7中:
override func viewDidLayoutSubviews() {
self.Loader.center = self.view.center
}