【问题标题】:Why is my UIButton not getting added to my cell?为什么我的 UIButton 没有被添加到我的单元格中?
【发布时间】:2013-11-03 07:52:15
【问题描述】:

在我的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 的新手,所以谁能弄清楚我做错了什么?

奇怪的是,如果我点击按钮应该是方法执行的区域。

【问题讨论】:

  • 为所有异常添加一个异常断点并运行你的应用程序,如果你的自动布局规则有一些冲突,应该停止执行并且你应该能够理解什么是错误的

标签: ios objective-c uibutton autolayout nslayoutconstraint


【解决方案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]];

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 你还需要使用- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state来改变文字颜色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多