ios - 调整 UITableViewCell 的 NSLayoutConstraint 错误

标签 ios objective-c uitableview autolayout nslayoutconstraint

我有一个自定义的 UITableViewCell,它带有一个使用自动布局调整大小的 subview 。此 subview 是单元格底部的工具栏。当单元格未被选中时它的高度为零,而在选中状态下增长到 30。单元格在 100 和 130 之间切换。

这是初始化:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _toolbarView = [[FOToolbarView alloc] init];
        _toolbarView.translatesAutoresizingMaskIntoConstraints = NO;
        _toolbarView.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview: _toolbarView];

        [self setNeedsUpdateConstraints];
    }
    return self;
}

这里是约束条件

- (void)updateConstraints
{
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeWidth
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeWidth
                                                                 multiplier: 1.0
                                                                   constant: 0]];
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeLeft
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeLeft
                                                                 multiplier: 1.0
                                                                   constant: 0]];
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeTop
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeTop
                                                                 multiplier: 0.0
                                                                   constant: 100.0]];
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeBottom
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeBottom
                                                                 multiplier: 1.0
                                                                   constant: 0]];
    [super updateConstraints];
}

布局按预期工作,但出现以下错误:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x1f810f50 FOToolbarView:0x1f812900.top == + 70>",
    "<NSLayoutConstraint:0x1f810dc0 FOToolbarView:0x1f812900.bottom == UITableViewCellContentView:0x1f8286d0.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f821230 h=--& v=--& V: [UITableViewCellContentView:0x1f8286d0(69)]>"
)

我尝试了很多东西,但都没有成功。我怎样才能摆脱这个错误?

最佳答案

问题似乎是系统自动添加了来自 tableviewcell.contentview 的 autoresizingmask 约束。试试这个:

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

关于ios - 调整 UITableViewCell 的 NSLayoutConstraint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21416306/

相关文章:

ios - 如何在非 tableView 方法中访问 tableView?

ios - Xamarin.iOS 未看到对 iOS 绑定(bind)库的引用

iphone - 在具有多个变量的另一个类中调用方法的正确语法是什么?

ios - 尝试用 nsmutablearray 呈现 uitableview

swift - 为什么两个连续的页面打开时带有 segue

swift - 我如何在核心数据中实现父子关系?

ios - 有没有办法观察在对象(iOS)上调用的每个消息调用?

ios - Swift 中的自定义组件

iphone - 在 webview 显示本地 HTML 页面之前几毫秒出现空白屏幕

objective-c - iOS 编程最佳实践 - 拼图帮助