【问题标题】:How to center UIActivityIndicator?如何使 UIActivityIndi​​cator 居中?
【发布时间】:2012-07-20 05:05:24
【问题描述】:

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

嘿嘿,

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

【问题讨论】:

    标签: iphone ios xcode ios5


    【解决方案1】:

    两个问题:

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

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

    【讨论】:

    • 感谢@Robert Ryan!摆脱你的两个提示,我能够让它正常工作。我必须找到导航栏和标签栏的高度才能获得合适的高度。我将代码添加到我的原始帖子中。
    【解决方案2】:

    SWIFT

    由于导航栏和标签栏,我遇到了同样的问题。要解决此问题,请将以下代码放入您的 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)
    

    【讨论】:

      【解决方案3】:

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

      试试这个

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • UIView 的边界中心不是和屏幕中心一样吗?
        • NSLog 告诉我 self.view 和 self.scrollView 的 CGRect 都是 {{0, 0}, {320, 460}}。并且 self.view.center 正确打印 (160, 230)。不知道这里出了什么问题。
        【解决方案5】:

        在 viewDidLayoutSubviews 中设置中心。

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

        }

        【讨论】:

          猜你喜欢
          • 2014-01-20
          • 1970-01-01
          • 1970-01-01
          • 2012-06-21
          • 1970-01-01
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多