如何活动的指标定位到使用自动布局编程它的父的中心? [英] How to position activity indicator to the center of its superview using Auto Layout programmatically?

查看:113
本文介绍了如何活动的指标定位到使用自动布局编程它的父的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下code没有定位活动指示灯工作,它的父的中心:

Why the following code does not work for positioning activity indicator to the center of its superview:

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]
       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

[self.mysuperview addSubview:activityIndicator];
[activityIndicator addConstraints:[NSLayoutConstraint 
                   constraintsWithVisualFormat:@"|-(>=20)-[view(==100)]-(>=20)-|"
                   options:NSLayoutFormatAlignAllCenterX | NSLayoutFormatAlignAllCenterY
                   metrics:nil
                   views:@{@"view" : self.mysuperview}]];

活动指示灯在左上角某处定位时,绝对不是在中心

Activity indicator is positioned somewhere at the top left corner, definitely not in the centre.

==================
更新:
发现其解决:我创建指标后关闭自动尺寸调整的约束,然后将所有的解决方案,在这里给定的工作:

================== Update: FOUND SOLUTION: I have to turn off autoresizing constraints after creating indicator and then all solutions that where given work:

 [activityIndicator setTranslatesAutoresizingMaskIntoConstraints:NO];

我发现它被赋予@Vignesh的链接,所以我接受他/她的回答。

I found it on the link given by @Vignesh, so I accept his/her answer.

推荐答案

您可以试试这个,

 UIView *superview = self.mysuperview;
NSDictionary *variables = NSDictionaryOfVariableBindings(activityIndicator, superview);
NSArray *constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[activityIndicator]"
                                        options: NSLayoutFormatAlignAllCenterX
                                        metrics:nil
                                          views:variables];
[self.view addConstraints:constraints];

constraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[activityIndicator]"
                                        options: NSLayoutFormatAlignAllCenterY
                                        metrics:nil
                                          views:variables];
[self.view addConstraints:constraints];

这里服用。

这篇关于如何活动的指标定位到使用自动布局编程它的父的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆