6

已解决:检查以下问题的解决方案。

在此处输入图像描述

嘿,所以,

我无法将 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];

关于如何获得中心 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);

在此处输入图像描述

4

5 回答 5

7

两个问题:

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

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

于 2012-07-20T22:35:57.313 回答
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)
于 2016-05-03T20:34:11.040 回答
4

使用 spinner.center = self.scrollView.center; 反而

试试这个

spinner.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));

于 2012-07-20T22:18:32.027 回答
3

我不确定到底发生了什么,但也许你的 UIScrollView 比屏幕高?您可以尝试打印 UIView 的大小以确定是否在屏幕范围内。也许 UIActivityIndi​​cator 位于 UIView 的中心(而不是屏幕的中心)。

于 2012-07-20T22:05:22.683 回答
0

在 viewDidLayoutSubviews 中设置中心。

    - (void) viewDidLayoutSubviews {

self.myActivityIndicator.center = self.view.center;    

}

于 2017-03-21T06:06:32.807 回答