簡體   English   中英

自動調整大小UITableViewCell:無法同時滿足約束

[英]Auto-resize UITableViewCell: Unable to simultaneously satisfy constraints

我正在嘗試實現一個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)>

在此輸入圖像描述

在此輸入圖像描述

在此輸入圖像描述

為了完整起見,這是我用於測試的簡單代碼。

“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 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM