ios 在 Objective-C 中以编程方式添加约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30590903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding constraints programmatically in Objective-C
提问by user3783005
I need to add some constraints to a UIButton
programmatically.
I need it to have it centred horizontally to superview, centred vertically to superview, aspect ratio to its superview and aspect ratio to self.
我需要以UIButton
编程方式向 a 添加一些约束。我需要它让它水平居中到超级视图,垂直居中到超级视图,纵横比到它的超级视图和纵横比到自我。
Can anyone help me please?
有人可以帮我吗?
Thank you.
谢谢你。
回答by Daniel T-D
I'll get the ball rolling for you so you can see the general idea, otherwise use the documentation as provided by Larme.
我会为您解决问题,以便您了解总体思路,否则请使用 Larme 提供的文档。
Add the constraint in your superview (probably your view controller).
在您的超级视图(可能是您的视图控制器)中添加约束。
NSLayoutConstraint *centreHorizontallyConstraint = [NSLayoutConstraint
constraintWithItem:self.uiButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
[self.view addConstraint:centreHorizontallyConstraint];
So as you can see we are saying constraint the centre x attribute of UIButton too the centre x attribute of the View Controllers view with no additional offsets (multiplier set to 1.0 and constant 0).
所以正如你所看到的,我们说约束 UIButton 的 center x 属性也是 View Controllers 视图的 center x 属性,没有额外的偏移(乘数设置为 1.0 和常量 0)。
Make sure you add it to your view controller's view not the button because the button has not been laid out at this point and therefore you cannot add a constraint to it! (Please someone correct me if I'm wrong here). I add my constraints in the viewDidLoad method.
确保将它添加到视图控制器的视图而不是按钮,因为此时按钮尚未布局,因此您无法向其添加约束!(如果我在这里错了,请有人纠正我)。我在 viewDidLoad 方法中添加了我的约束。
回答by Vaibhav Saran
I found this tutorial best.
我发现这个教程最好。
http://www.tutorialspoint.com/ios/ios_auto_layouts.htm
http://www.tutorialspoint.com/ios/ios_auto_layouts.htm
My requirement was to set:
我的要求是设置:
- Leading Space
- Top Space
- fixed Width
- fixed Height
- 领先空间
- 顶部空间
- 固定宽度
- 固定高度
Adding NSLayoutConstraint
with the code below I was able to achieve it
添加NSLayoutConstraint
下面的代码我能够实现它
self.btnCoin.translatesAutoresizingMaskIntoConstraints = NO;
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:self.btnCoin attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:self attribute:
NSLayoutAttributeLeft multiplier:1.0 constant:30];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:self.btnCoin attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self attribute:
NSLayoutAttributeTop multiplier:1.0f constant:258];
/* Fixed width */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.btnCoin
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:35];
/* Fixed Height */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.btnCoin
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:12];
/* 4. Add the constraints to button's superview*/
[self addConstraints:@[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
In the code above i set left button constraints
. Here is the output:
在上面的代码中,我设置了 left button constraints
。这是输出:
回答by Lal Krishna
We have better option available from iOS 9+
我们在 iOS 9+ 上有更好的选择
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:imageView];
[imageView.widthAnchor constraintEqualToConstant:imageSize.width].active = YES;
[imageView.heightAnchor constraintEqualToConstant:imageSize.height].active = YES;
[imageView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[imageView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:25].active = YES;
Swift 4
斯威夫特 4
var imageView = UIImageView(frame: CGRect.zero)
imageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imageView)
imageView.widthAnchor.constraint(equalToConstant: imageSize.width).isActive = true
imageView.heightAnchor.constraint(equalToConstant: imageSize.height).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 25).isActive = true
// OR you can use: NSLayoutConstraint.activate([NSLayoutConstraint]) to activate all at once.
NB: Always add constrains after added the view to view hierarchy.
注意:在将视图添加到视图层次结构后始终添加约束。