如何使UIActivityIndi​​cator居中?

kkS*_*der 6 iphone xcode ios ios5

已解决:检查下面的问题解决方案.

在此输入图像描述

嘿SO,

我无法将UIActivityIndi​​cator集中在视图的中心.正如你在这里看到的那样,它在垂直方向上比它应该更远.我所拥有的是一个UIScrollView,后来添加了一个UIImage子视图.在加载UIImage之前,我有这个UIActivityIndi​​cator来显示图像正在加载.

- (void)viewDidLoad
{

    [super viewDidLoad];
    self.scrollView.backgroundColor = [UIColor blackColor];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.center = self.view.center;

    [spinner startAnimating];
    [self.view addSubview:spinner];
Run Code Online (Sandbox Code Playgroud)

关于如何获得中心CGPoint并在那里设置UIActivityIndi​​cator的任何想法?我不确定为什么self.view.center不起作用.

提前致谢.

编辑:我必须考虑导航栏和标签栏的高度.我必须添加的代码是:

float navigationBarHeight = [[self.navigationController navigationBar] frame].size.height;
float tabBarHeight = [[[super tabBarController] tabBar] frame].size.height;
spinner.center = CGPointMake(self.view.frame.size.width / 2.0, (self.view.frame.size.height  - navigationBarHeight - tabBarHeight) / 2.0);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Rob*_*Rob 7

两个问题:

  1. 首先,self.view.center是当前视图框架在其父框架上的中心.如果您想要当前视图的中心,则需要CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);

  2. 其次,您当前的视图不包含导航栏,因此您将其置于导航栏下方屏幕的空间中心.这也会让它看起来更低.


小智 5

迅速

由于导航栏和标签栏,我遇到了同样的问题.要修复此问题,请将以下代码放在loadView()中或在您​​调用活动指示符的任何位置开始设置动画:

let navigationBarHeight = self.navigationController?.navigationBar.frame.height
let tabBarHeight = super.tabBarController!.tabBar.frame.height
YourActivityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, (self.view.frame.size.height - navigationBarHeight! - tabBarHeight) / 2.0)
Run Code Online (Sandbox Code Playgroud)