I'm attempting to implement a UITableViewCell that automatically resizes its height to fit the available contents. I have the following layout right now, but whenever I'm running the program, the debugger throws a variety of "Unable to simultaneously satisfy constraints" errors. Is there something wrong with the way I'm setting up my constraints?
[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)>
For completeness, here's the simple code that I'm using for testing.
"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;
}
}
The error says that there are some other constraint that conflicts with your imageview's height 60, and this constraint is modified so that all constraints can be satisfied.
You can either tweak around to see which constraint conflict with that height, or you can just break it by yourself. To do so, click on the edit button on image height, and then change priority to 999
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With