iOS - 以编程方式使用自动布局将 UIView 定位在超级视图的中心 [英] iOS - Position UIView in center of superview using Auto Layout programmatically

查看:18
本文介绍了iOS - 以编程方式使用自动布局将 UIView 定位在超级视图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Auto Layout 以编程方式将 UIView 设置为位于其父视图的中心?

How do you programmatically set a UIView to be in the center of its superview using Auto Layout?

UIButton* viewObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewObj setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewObj setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:viewObj];

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:viewObj
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterX 
                                                     multiplier:1.0
                                                       constant:0];
[self.view addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:viewObj 
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual 
                                     toItem:self.view
                                  attribute:NSLayoutAttributeCenterY
                                 multiplier:1.0
                                   constant:0];
[self.view addConstraint:cn];

上面的代码适用于 UIButton,但我无法用适用于 UIView 的内容替换第一行.

The above code works for me for UIButton, but I'm having trouble replacing the first line with something that works for a UIView.

我试过了

UIView* viewObj = [UIView alloc] initWithFrame:CGRectZero];

但视图没有显示在模拟器中.

but the view does not show up in simulator.

有什么建议吗?

谢谢!

推荐答案

由于您在使用 Auto Layout 的上下文中提出了这个问题,这里的问题是 UIButton 有一个固有大小(通过 intrinsicContentSize 方法通信),它提供带有宽度和高度信息的自动布局,但 UIView 通常没有.所以你需要添加更多与宽度和高度相关的约束.

Since you asked this question in the context of using Auto Layout, the issue here is that a UIButton has an intrinsic size (communicated through the intrinsicContentSize method) that provides Auto Layout with information about width and height, but a UIView normally does not. So you need to add more constraints related to width and height.

如果你希望你的 UIView 是一个固定的尺寸(比如,200x200),你可以添加这些行:

If you want your UIView to be a set size (say, 200x200), you could add these lines:

cn = [NSLayoutConstraint constraintWithItem:viewObj 
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual 
                                     toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute 
                                 multiplier:1
                                   constant:200];
[viewObj addConstraint:cn];

cn = [NSLayoutConstraint constraintWithItem:viewObj
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute 
                                 multiplier:1 
                                   constant:200];
[viewObj addConstraint: cn];

注意 toItem: 参数是 nil 而第二个属性是 NSLayoutAttributeNotAnAttribute,因为您没有指定相对的宽度和高度其他任何事情.如果您希望子视图的高度和宽度相对于父视图(例如 0.5),您可以这样做:

Note that the toItem: argument is nil and the second attribute is NSLayoutAttributeNotAnAttribute, because you aren't specifying the width and height relative to anything else. If you want the subview's height and width to be relative to the superview (say, 0.5), you could do this:

cn = [NSLayoutConstraint constraintWithItem:viewObj
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:self.view
                                  attribute:NSLayoutAttributeHeight 
                                 multiplier:0.5 
                                   constant:0];
[self.view addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:viewObj
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:self.view
                                  attribute:NSLayoutAttributeWidth
                                 multiplier:0.5
                                   constant:0];
[self.view addConstraint: cn];

这篇关于iOS - 以编程方式使用自动布局将 UIView 定位在超级视图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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