为什么我的UIActivityIndi​​catorView不在屏幕中心?

时间:2011-05-04 16:49:46

标签: iphone ios uiactivityindicatorview

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);

目前它出现在下半部分。

6 个答案:

答案 0 :(得分:16)

这会将指示器放在视图的中心(而不是屏幕!):

activityIndicator.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);
activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)

当尺寸发生变化时(例如切换屏幕方向时),自动调整功能会保留在中央。

如果它不起作用,则问题出在其他地方。尝试记录视图的大小,也许它的位置不正确?

NSLog(@"View frame: %@", NSStringFromCGRect(self.view.frame));

答案 1 :(得分:2)

你可以这样做:

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.frame = CGRectMake(self.view.bounds.size.width / 2.0f - activityIndicator.frame.size.width /2.0f, self.view.bounds.size.height / 2.0f - activityIndicator.frame.size.height /2.0f, activityIndicator.frame.size.width, activityIndicator.frame.size.height);
//then add to the view

答案 2 :(得分:2)

为什么不这样呢

activityIndicator.center =  self.view.center;

activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin)

这很好,而不必像其他人那样进行那些计算

答案 3 :(得分:1)

如果要向parentView添加指示符,则逻辑应将指示符置于父级中心 - 它适用于我。

 // ...
 CGSize parentSize = parentView.frame.size;
 UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
 activityIndicator.center = CGPointMake(parentSize.width/2, parentSize.height/2);
 // ...

答案 4 :(得分:0)

在viewDidLayoutSubviews中设置中心。

- (void) viewDidLayoutSubviews {
self.myActivityIndicator.center = self.view.center; }

谢谢:)

答案 5 :(得分:-3)

我也有这个问题,试试这个

activityIndicator.center =CGPointMake(480/2.0, 320/2.0);