我正在使用自动布局编写一个 iOS 应用程序。 我想在屏幕 Y 中心下方 30 点处放置一个按钮。 我如何为此添加约束?
最佳答案
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *superView = self.view;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
[button setTitle:@"Button" forState:UIControlStateNormal];
[superView addSubview:button];
// adding constraints to the button
NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:30];// adds 30 points from Screen Center Y
[superView addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0];// places the button in middle of X axis
[superView addConstraint:cn];
}
关于iPhone5 Storyboard自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719918/