在我的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]];

但是当我加载单元时它永远不会显示。我是自动布局的新手,所以有人可以找出我做错了什么吗?

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

最佳答案

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

10-06 07:54
查看更多