为什么我的UIButton没有被添加到我的手机中?

时间:2013-10-24 14:29:59

标签: ios objective-c uibutton autolayout nslayoutconstraint

在我的cellForRowAtIndexPath:方法中,我有以下代码:

        UIButton *signOutButton = [[UIButton alloc] init];

        [signOutButton addTarget:self action:@selector(logoutButtonTapped) forControlEvents:UIControlEventTouchUpInside];
        signOutButton.translatesAutoresizingMaskIntoConstraints = NO;
        signOutButton.titleLabel.textColor = [UIColor colorWithRed:0/255.0 green:180/255.0 blue:35/255.0 alpha:1.0];
        signOutButton.titleLabel.text = @"Sign Out";

        [cell.contentView addSubview:signOutButton];

        [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton
                                                                     attribute:NSLayoutAttributeLeading
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:cell.contentView
                                                                     attribute:NSLayoutAttributeLeft
                                                                    multiplier:1.0
                                                                      constant:50.0]];

        [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton
                                                                     attribute:NSLayoutAttributeTrailing
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:cell.contentView
                                                                     attribute:NSLayoutAttributeRight
                                                                    multiplier:1.0
                                                                      constant:50.0]];

        [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton
                                                                     attribute:NSLayoutAttributeBottom
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:cell.contentView
                                                                     attribute:NSLayoutAttributeBottom
                                                                    multiplier:1.0
                                                                      constant:-6.0]];

但是当我加载单元格时它永远不会显示出来。我对Auto Layout有点新意,所以有人能弄清楚我做错了吗?

如果我点击方法执行按钮的区域,那就太奇怪了。

2 个答案:

答案 0 :(得分:3)

由于按下它时执行该方法,似乎您的按钮实际上已添加到单元格中。我认为标题没有设定。尝试更换     signOutButton.titleLabel.text = @"Sign Out" 同     [signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal]

答案 1 :(得分:1)

您的按钮已添加,因为您的标签标题未设置,因此您没有看到它。您可以更改背景颜色以查看单元格中的位置:

[signOutButton setBackgroundColor:[UIColor blueColor]];

然后更改标题和颜色如下:

[signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal];
[signOutButton setTitleColor:[UIColor colorWithRed:0/255.0 green:180/255.0 blue:35/255.0 alpha:1.0] forState:UIControlStateNormal];

这是您最终代码的样子:

    UIButton *signOutButton = [[UIButton alloc] init];

[signOutButton addTarget:self action:@selector(logoutButtonTapped) forControlEvents:UIControlEventTouchUpInside];
signOutButton.translatesAutoresizingMaskIntoConstraints = NO;
[signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal];
[signOutButton setTitleColor:[UIColor colorWithRed:0/255.0 green:180/255.0 blue:35/255.0 alpha:1.0] forState:UIControlStateNormal];
[signOutButton setBackgroundColor:[UIColor blueColor]]; //just for testing, you can get rid of this line

[cell.contentView addSubview:signOutButton];

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton
                                                             attribute:NSLayoutAttributeLeading
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:cell.contentView
                                                             attribute:NSLayoutAttributeLeft
                                                            multiplier:1.0
                                                              constant:50.0]];

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton
                                                             attribute:NSLayoutAttributeTrailing
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:cell.contentView
                                                             attribute:NSLayoutAttributeRight
                                                            multiplier:1.0
                                                              constant:50.0]];

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:signOutButton
                                                             attribute:NSLayoutAttributeBottom
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:cell.contentView
                                                             attribute:NSLayoutAttributeBottom
                                                            multiplier:1.0
                                                              constant:-6.0]];