iOS版 - 位置的UIView在上海华中心使用自动布局编程 [英] iOS - Position UIView in center of superview using Auto Layout programmatically
问题描述
你如何编程方式设置一个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];
以上code对我的作品进行的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.
有什么建议?
谢谢!
推荐答案
既然你问在使用自动布局的背景下,这个问题,这里的问题是,一个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是一个集的大小(例如,200×200),您可以添加这些行:
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:
参数是无
第二属性是 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屋!