自動レイアウトを使用して iOS アプリを作成しています。画面Y中央から30ポイント下にボタンを配置したいです。これに制約を追加するにはどうすればよいですか?
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 に答える