我正在使用自动布局编写一个 iOS 应用程序。我想在屏幕 Y 中心下方 30 点处放置一个按钮。如何为此添加约束?
问问题
399 次
1 回答
2
- (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];
}
于 2013-02-06T05:36:53.257 回答