首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone5情节提要自动布局

iPhone5情节提要自动布局
EN

Stack Overflow用户
提问于 2013-02-06 09:11:21
回答 1查看 399关注 0票数 2

我正在写一个使用自动布局的iOS应用程序。我想在屏幕Y中心的30个点上放置一个按钮。我如何为此添加约束?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-06 13:36:53

代码语言:javascript
复制
- (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];

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14719918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档