【问题标题】:Trying to add a UIButton to my view with Auto Layout, but it's not appearing. Why? [closed]尝试使用自动布局将 UIButton 添加到我的视图中,但它没有出现。为什么? [关闭]
【发布时间】:2013-06-29 02:31:07
【问题描述】:

我有以下代码:

- (void)addLogoutButton {
    UIButton *logoutButton = [[UIButton alloc] init];
    logoutButton.translatesAutoresizingMaskIntoConstraints = NO;
    [logoutButton setTitle:@"Logout" forState:UIControlStateNormal];
    [self.view addSubview:logoutButton];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoutButton
                                                 attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeTop
                                                 multiplier:1.0
                                                 constant:-100.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoutButton
                                                 attribute:NSLayoutAttributeLeft
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeLeft
                                                 multiplier:1.0
                                                 constant:40.0]];


    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:logoutButton
                                                 attribute:NSLayoutAttributeRight
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeRight
                                                 multiplier:1.0
                                                 constant:-40]];
}

但是当我运行应用程序时,找不到该按钮。这是为什么呢?

【问题讨论】:

    标签: ios objective-c autolayout nslayoutconstraint


    【解决方案1】:

    将您的第一个约束常量更改为 +100.0 并使用以下命令初始化您的按钮:

    [UIButton buttonWithType:UIButtonTypeRoundedRect];
    

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2013-02-01
      • 1970-01-01
      相关资源
      最近更新 更多