A self-sizing UITableViewCell: MyTableViewCell.m
-(void) viewDidLoad{
UIView *sample = [[UIView alloc] init];
[self.contentView addSubview: sample];
//Fake code here. Let sample view fill the whole contentView:
Constraint1: sample.leading = contentView.leading;
Constraint2: sample.trailing = contentView.trailing;
Constraint3: sample.top = contentView.top;
Constraint4: sample.bottom = contentView.bottom;
Constraint5: sample.height = 20;
NSLayoutConstraint:activateConstriant(constraints 1-5);
}
This code works and the MyTableViewCell
has a height of 20.
However, I want to change the height at run time (after this cell added to a UITableView), so I add a new method to MyTableViewCell.m
-(void) updateHeight:(CGFloat)newHeight{
//Fake code here. change the constant of constraint1
Constraint1 = get reference of Constraint1 in viewDidLoad();
Constraint1.constant = newHeight
}
I run the code and call updateHeight:10
and got the log warning:
2017-03-16 16:23:11.102858 [14541:568021] [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. ( "", "", "", "" ) Will attempt to recover by breaking constraint
My own opinion is that my constraints created at viewDidLoad
automatically creates couple of other constraints like contentView.height=20
and my change to the original constraints conflict with the constraints created by system.
I tested to change the leading of sample view instead of the height in updateHeight:
, and it works! Because the change of leading doesn't affect contentView's own height constraint?
How am I supposed to do a dynamically change to auto layout like this case?
Thank you~~~