iPhone5故事板自动布局

时间:2013-02-06 01:11:21

标签: iphone ios6 retina-display autolayout

我正在使用autolayout编写iOS应用。 我想在屏幕Y中心下方的30个点放置一个按钮。 如何为此添加约束?

1 个答案:

答案 0 :(得分: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];

}
相关问题