Hi smileyborg,
First, thanks a ton for creating PureLayout and this TableViewCellWithAutoLayout sample. It's a huge help.
Your sample app works great. No errors, everything looks good in the simulator. My strategy is to add as little as possible to your sample to understand how each piece works, but I'm hitting an 'Unable to simultaneously satisfy constraints' error almost immediately.
All I am trying to do is insert a UIView with a colored background (like in your example apps) in between the titleLabel and bodyLabel. I can do it, but not without XCode complaining and breaking a constraint.
I started by adding a new UIView to RJTableViewCell.h:
@property (strong, nonatomic) IBOutlet UIView *storyView;
Then I instantiated storyView in initWithStyle:reuserIdentifier by adding these lines just before where contentView's background color is set:
self.storyView = [UIView newAutoLayoutView];
self.storyView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.1];
Then in updateConstraints, I uncommented:
self.contentView.bounds = CGRectMake(0.0f, 0.0f, 99999.0f, 99999.0f);
and added this after the calls on self.titleLabel:
[self.storyView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationEqual];
[UIView autoSetPriority:UILayoutPriorityRequired forConstraints:^{
[self.storyView autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
}];
[self.storyView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:kLabelHorizontalInsets];
[self.storyView autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:kLabelHorizontalInsets];
[self.storyView autoSetDimension:ALDimensionHeight toSize:25.0];
And finally, I changed:
[self.bodyLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationGreaterThanOrEqual];
to:
[self.bodyLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.storyView withOffset:kLabelVerticalInsets relation:NSLayoutRelationGreaterThanOrEqual];
I get what I expected -- a blue-green rectangle 25 pixels high exactly as wide as titleLabel and bodyLabel with 10 pixels of space above and below. I also get this:
2015-01-28 19:29:43.847 TableViewCellWithAutoLayout[41969:3383075] 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:0x7f8da3036bb0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f8da302b860(44)]>",
"<NSLayoutConstraint:0x7f8da3037ec0 V:|-(10)-[UILabel:0x7f8da302c900'Marion'] (Names: '|':UITableViewCellContentView:0x7f8da302b860 )>",
"<NSLayoutConstraint:0x7f8da3038390 V:[UILabel:0x7f8da302c900'Marion']-(10)-[UIView:0x7f8da302d350]>",
"<NSLayoutConstraint:0x7f8da30369f0 V:[UIView:0x7f8da302d350(25)]>",
"<NSLayoutConstraint:0x7f8da3036b20 V:[UIView:0x7f8da302d350]-(>=10)-[UILabel:0x7f8da302cf20'Lorem ipsum dolor sit ame...']>",
"<NSLayoutConstraint:0x7f8da3036590 UILabel:0x7f8da302cf20'Lorem ipsum dolor sit ame...'.bottom == UITableViewCellContentView:0x7f8da302b860.bottom - 10>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f8da30369f0 V:[UIView:0x7f8da302d350(25)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
I've experimented with every variation I can think of. No matter what I do, I get this constraint problem in XCode. It doesn't seem like I've done much to your sample, but I assume I've creating ambiguity somewhere. If you can shed any light on it, I'd be grateful.
Thanks in advance,
Dan