ios - 自动调整 UITableViewCell : Unable to simultaneously satisfy constraints

标签 ios objective-c xcode uitableview uikit

我正在尝试实现一个 UITableViewCell,它会自动调整其高度以适应可用内容。我现在有以下布局,但每当我运行程序时,调试器都会抛出各种“无法同时满足约束”错误。我设置约束的方式有问题吗?

[LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x60800009c2f0 UIImageView:0x7fd389002f50.height == 60   (active)>",
    "<NSLayoutConstraint:0x60800009a8b0 UIImageView:0x7fd389002f50.top == UITableViewCellContentView:0x7fd389009b20.topMargin + 4   (active)>",
    "<NSLayoutConstraint:0x608000097ca0 UITableViewCellContentView:0x7fd389009b20.bottomMargin >= UIImageView:0x7fd389002f50.bottom + 4   (active)>",
    "<NSLayoutConstraint:0x600000097d40 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fd389009b20.height == 80   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60800009c2f0 UIImageView:0x7fd389002f50.height == 60   (active)>

enter image description here

enter image description here

enter image description here

为了完整性,这里是我用于测试的简单代码。

"ListViewController.m"

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 40;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ListTableViewCell *cell = (ListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath];

    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

- (void)configureCell:(ListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.hasProfile = (arc4random_uniform(10) < 3);
    cell.hasSecondField = (arc4random_uniform(10) < 3);
}

ListViewCell.h

@interface ListTableViewCell : UITableViewCell {
    IBOutlet NSLayoutConstraint *pictureWidthConstraint;
    IBOutlet NSLayoutConstraint *pictureHeightConstraint;
    IBOutlet NSLayoutConstraint *pictureBottomTrailingConstraint;
    IBOutlet NSLayoutConstraint *subheaderHeightConstant;

    IBOutlet UILabel *subheaderLabel;
}

@property (nonatomic, assign) BOOL hasProfile;
@property (nonatomic, assign) BOOL hasSecondField;

@end

ListViewCell.m

- (void)setHasProfile:(BOOL)hasProfile {
    _hasProfile = hasProfile;

    if (!hasProfile) {
        pictureHeightConstraint.constant = 0;
        pictureWidthConstraint.constant = 0;
    }
    else {
        pictureHeightConstraint.constant = 60;
        pictureWidthConstraint.constant = 60;
    }
}

- (void)setHasSecondField:(BOOL)hasSecondField {
    _hasSecondField = hasSecondField;

    if (!hasSecondField) {
        subheaderLabel.text = @"";
        subheaderHeightConstant.constant = 0;
    }
    else {
        subheaderLabel.text = @"Second Label";
        subheaderHeightConstant.constant = 21;
    }
}

最佳答案

错误说有一些其他约束与您的 imageview 的高度 60 冲突,并且修改此约束以便可以满足所有约束。

您可以四处调整以查看哪个约束与该高度冲突,或者您可以自己打破它。为此,请单击图像高度上的编辑按钮,然后将优先级更改为 999 enter image description here

关于ios - 自动调整 UITableViewCell : Unable to simultaneously satisfy constraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689647/

相关文章:

ios - 如何让 iOS 应用程序在 iPhone 5、iPhone 6 和 iPhone 6+ 上看起来一样

ios - iCloud 正在备份我的应用程序的 68 字节(一个 plist 文件),我是否需要设置一个标志以将其从 icloud 备份中排除?

ios - 如何发出多个 GET 请求并等待所有操作完成?

xcode - OS X 应用程序中带有 TabViewController 的 Storyboard - 每个场景中的核心数据数组 Controller ?

ios - AVCaptureDevice 的 hasFlash 和 hasTorch 属性有什么区别?

iOS 应用元数据被拒绝?有什么要检查 ViewDidLoad 的吗?

ios - 无法在 swift 3 的结构中获取返回值

objective-c - NSString 分配中的摘要无效

iOS 将数据库更新移出主线程 - 插入启动 View

ios - 无需模拟器即可运行 iOS 代码