简体   繁体   English

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

[英]Why is my UIButton not getting added to my cell?

In my cellForRowAtIndexPath: method I have the following code: 在我的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]];

But it never shows up when I load the cell. 但是当我加载单元格时它永远不会出现。 I'm sort of new to Auto Layout, so can anyone figure out what I'm doing wrong? 我对Auto Layout有点新意,所以有人能弄清楚我做错了什么吗?

Oddly enough if I tap the area where the button should be the method executes. 奇怪的是,如果我点击按钮应该执行的区域。

Since the method is executed when you press it, it seems that your button is actually added to the cell. 由于该方法在您按下时执行,因此您的按钮实际上似乎已添加到单元格中。 I think that the title is not set. 我认为标题没有设定。 Try replacing signOutButton.titleLabel.text = @"Sign Out" with [signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal] 尝试用[signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal]替换signOutButton.titleLabel.text = @"Sign Out"

Your button is getting added, since your label title is not getting set therefore you are not seeing it. 您的按钮正在添加,因为您的标签标题未设置,因此您没有看到它。 You can change the background color to see where is it in the cell as: 您可以更改背景颜色以查看单元格中的位置:

[signOutButton setBackgroundColor:[UIColor blueColor]];

And then change title and color as following: 然后更改标题和颜色如下:

[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];

This how your final code is going to look like: 这是你的最终代码如下所示:

    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]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM