Getting "Unable to simultaneously satisfy constraints" after updating constraint in iOS 7 - ios

I've got a UITableViewController, with a custom view, with some subviews and controls inside, in the header of the table. Each one of these subviews are a custom subview (use a custom subview to draw corner radius with IBInspectable) and each have constraints for top, bottom, leading and trailing space (all set to 8) as well as for height (set to 60, 80 or 100, depending of each subview).
One of these subviews constraints can be modified programmatically in run time depending of user interaction. To do that, I create this two methods:
- (void)showSearchTypeTermField:(BOOL)animated
{
self.searchTypeHeightConstraint.identifier = #"Height 100";
[self.searchTypeHeightConstraint setConstant:100.0];
if (animated) {
[self.tableView.tableHeaderView layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.tableView.tableHeaderView layoutIfNeeded];
}];
} else {
[self.tableView.tableHeaderView layoutIfNeeded];
}
self.searchTypeTermField.hidden = NO;
self.searchTypeTermField.text = #"";
[self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
}
- (void)hideSearchTypeTermField:(BOOL)animated
{
self.searchTypeHeightConstraint.identifier = #"Height 60";
[self.tableView.tableHeaderView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[self.searchTypeHeightConstraint setConstant:60.0];
if (animated) {
[self.tableView.tableHeaderView layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.tableView.tableHeaderView layoutIfNeeded];
}];
} else {
[self.tableView.tableHeaderView layoutIfNeeded];
}
self.searchTypeTermField.hidden = YES;
[self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
}
Because all of these subviews are in the table header view, every time I change the constraint, header should expand or compress depending of the case, thats why I use self.tableView.tableHeaderView systemLayoutSizeFittingSize.
The problem is I'm getting this error, when I call [self hideSearchTypeTermField:NO] the first time on viewdidLoad despite that visually everything seems fine:
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:0x7bf504e0 V:[SeachFormBackgroundView:0x7bea3ab0(60)]>",
"<NSLayoutConstraint:0x7bf51760 V:[SeachFormBackgroundView:0x7bf50690(60)]>",
"<NSLayoutConstraint:0x7bf53390 'Height 60' V:[SeachFormBackgroundView:0x7bf518e0(60)]>",
"<NSLayoutConstraint:0x7bf53ec0 V:[SeachFormBackgroundView:0x7bf535a0(80)]>",
"<NSLayoutConstraint:0x7bf54a80 V:[SeachFormBackgroundView:0x7bf54180(80)]>",
"<NSLayoutConstraint:0x7bf54eb0 V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0] (Names: '|':UIView:0x7bea3a10 )>",
"<NSLayoutConstraint:0x7bf54f40 V:[SeachFormBackgroundView:0x7bea3ab0]-(8)-[SeachFormBackgroundView:0x7bf50690]>",
"<NSLayoutConstraint:0x7bf54f70 V:[SeachFormBackgroundView:0x7bf50690]-(8)-[SeachFormBackgroundView:0x7bf518e0]>",
"<NSLayoutConstraint:0x7bf55090 V:[SeachFormBackgroundView:0x7bf518e0]-(8)-[SeachFormBackgroundView:0x7bf535a0]>",
"<NSLayoutConstraint:0x7bf550f0 V:[SeachFormBackgroundView:0x7bf54180]-(8)-| (Names: '|':UIView:0x7bea3a10 )>",
"<NSLayoutConstraint:0x7bf55180 V:[SeachFormBackgroundView:0x7bf535a0]-(8)-[SeachFormBackgroundView:0x7bf54180]>",
"<NSAutoresizingMaskLayoutConstraint:0x7c149fc0 h=--& v=--& V:[UIView:0x7bea3a10(428)]>"
)
I'm really lost with this issue. What I'm doing wrong?
Thanks.

If you try adding the heights for the SeachFormBackgroundView instances (60 + 60 + 60 + 80 + 80 + 8 * 6 = 388 and their separators you'll see that they aren't equal to the parent UIView height (428). That's why you get this message.
You have to either adjust the constraints so that they add up to the parent view height, or resize the parent so that it's size matches the child constraints.
You don't need all of the constraints you have right now. Since you're setting a specific height to all of the subviews and spacing constraints, you only need to anchor them to the top or to the bottom of the superview.
Edit:
You don't need both of these constraints: V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0] and V:[SeachFormBackgroundView:0x7bf54180]-(8)-|. The first one anchors your subviews to the top of the superview, and the scond one anchors them to the bottom of your superview. Remove one of them (I'd expect the bottom one) and the views will fall in place without throwing any AutoLayout exceptions.

When you set the header view the table view reads its size and sets a constraint for that height. Updating the constraints on the header view itself don't cause a reevaluation of that height. You should remove the header view, update its constraints, lay it out and then add the header view again.

Related

Superview not increasing in height based on the subviews constraint

I have a scrollview and a separate UIView where I placed a series of textFields and labels with constraints which fully occupies the top and bottom. I'm trying to adjust the UIView's height based on its subview constraints but it won't. What is happening is that the view keeps its height and force other textfields to collapse or shrink thus breaking the constraints.
Details
Each subview priority values :
compression = 750
hugging = 250
UIView priority values:
compression = 249
hugging = 749 Set to be lower than the rest.
Most of the textfields has aspect ratio constraint. This causes the field to adjust.
Each subview has vertical/top/bottom spacing between each other. The top and bottom elements has top and bottom constraints to the view as well.
What's on my code:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
/* I had to adjust the UIView's width to fill the entire self.view.*/
if(![contentView isDescendantOfView:detailsScrollView]){
CGRect r = contentView.frame;
r.size.width = self.view.frame.size.width;
contentView.frame = r;
[detailsScrollView addSubview:contentView];
}
}
Screenshots
The view
This is what currently happens. In this instance it forces the email field to shrink. If I place a height value on it, it does not shrink but the layout engine finds another element to break
Edit:
Solved
Maybe I just needed some break to freshen up a bit. I did tried using constraints before but got no luck. However thanks to the suggestion I went back setting the constraints instead of setting the frame on this one and got it finally working.
Solution:
-(void)viewDidLoad{
[detailsScrollView addSubview:contentView];
[contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[detailsScrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(contentView,detailsScrollView);
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[contentView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil
views:viewsDictionary];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-0-[contentView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:viewsDictionary];
NSArray *widthConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-0-[contentView(==detailsScrollView)]-0-|" options:0 metrics:nil views:viewsDictionary];
}
When you use interface builder to deal with the UIScrollView and its child UIView. usually a top, bottom, left and equal width constraints are set between the UIScrollView and its child which is the contentView in your case.
Without those constraints the other option is to set the content size of the UIScrollView. which was the way of using the UIScrollView before introducing constraints.
So, 1. you should add those constraints programmatically.
By using the constraints, the views frame is no longer needed to resize the views.
So, 2. remove frame setting for your content view.
I am not so happy with the way you set the frame in the viewDidLayoutMethod. if I am going to do that here I would take the frame setting out of the if statement.
The code would be as follow with no if statement:
[detailsScrollView addSubview:contentView];
// then set the constraints here after adding the subview.
Put this code anywhere but not inside your viewDidLayoutSubviews method. it will be a bigger problem than setting the frame in there inside if statement.
Note: Originally, if you are going to set frame in the viewDidLayoutSubviews
method. you should do it for all cases. for example for the if case
and the else case. because, next time this method is going to be
called the views will respond to the constraint. and lose its frame.
Another observation: if you want the view to response to its subviews constraint why you need to set the frame for it? right?
After adding the constraint you may need to call the method constraintNeedsUpdate or another related method.

Can't Get Rid of Auto Constraints Warning

I was recently having an issue with animating a UITableView. It turned out that the issue was being caused by auto layout constraints. I disabled auto layout for the UITableView by using the following code:
myTableView.translatesAutoresizingMaskIntoConstraints = YES;
This solved the issue but now every time I animate the UITableView I break the constraints placed on the frame and get the following warning.
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)
(
"<NSIBPrototypingLayoutConstraint:0x170a95db0 'IB auto generated at build time for view with fixed frame' V:|-(538)-[UITableView:0x140330c00] (Names: '|':UIView:0x170199640 )>",
"<NSIBPrototypingLayoutConstraint:0x170c9d010 'IB auto generated at build time for view with fixed frame' V:[UITableView:0x140330c00(220)]>",
"<NSAutoresizingMaskLayoutConstraint:0x175e850a0 h=--& v=--& UITableView:0x140330c00.midY == + 458>",
"<NSAutoresizingMaskLayoutConstraint:0x175c97b10 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x170199640] (Names: '|':UITransitionView:0x143942020 )>"
)
Will attempt to recover by breaking constraint
<NSIBPrototypingLayoutConstraint:0x170a95db0 'IB auto generated at build time for view with fixed frame' V:|-(538)-[UITableView:0x140330c00] (Names: '|':UIView:0x170199640 )>
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 need to use translatesAutoresizingMaskIntoConstraints to make my animation work, but would like to get rid of this pesky warning. Is there any way to eliminate this warning without removing translatesAutoresizingMaskIntoConstraints = YES?
Edit in response to comments:
So the UITableView has a width of 320 and a height of 220. It covers half of the screen and can be minimized or maximized. The view is initialized with the UITableView in a minimized state with just the top section header showing at the bottom of the screen. When the user clicks on a button in the section header the UITableView is expanded form the bottom up to cover half of the viewable screen. The expansion and minimization of the UITableView is where the UITableView is animated. When the user clicks the maximize/minimize button the the y component of the UITableView frame is changed to move it accordingly. Here is the code I use to animate the table view:
UIImage *downImage = [UIImage imageNamed:#"arrowDown.png"];
UIImage *upImage = [UIImage imageNamed:#"arrowUp.png"];
UIButton *btnExpandCompress = (UIButton *)sender;
[btnExpandCompress setEnabled:NO];
if (showingTable)
{
[UIView animateWithDuration:0.7 animations:^{
CGRect frame = animatedTable.frame;
frame.origin.y += animatedTable.frame.size.height - 30;
animatedTable.frame = frame;
} completion:^(BOOL finished) {
showingTable = NO;
[btnExpandCompress setEnabled:YES];
[btnExpandCompress setBackgroundImage:upImage forState:UIControlStateNormal];
}];
}
else
{
[UIView animateWithDuration:0.7 animations:^{
CGRect frame = animatedTable.frame;
frame.origin.y -= animatedTable.frame.size.height - 30;
animatedTable.frame = frame;
} completion:^(BOOL finished) {
showingTable = YES;
[btnExpandCompress setEnabled:YES];
[btnExpandCompress setBackgroundImage:downImage forState:UIControlStateNormal];
}];
}
As can be seen above all I do is change the y component of the frame. Originally the frame of the UITableView has a y component of 583 which is mentioned in the original constraint warning <NSIBPrototypingLayoutConstraint:0x170a95db0 'IB auto generated at build time for view with fixed frame' V:|-(538)-[UITableView:0x140330c00] (Names: '|':UIView:0x170199640 )>. I don't set any additional constraints for the UITableView, just drag it from the object library and move it to its initial minimized position. I am guessing that by changing the frames y component I'm breaking a fixed constraint that is getting set by default. I hope this additional information helps. How can I fix this?
There is problem with your tableview
You have set multiple values for height of tableview
constraint -538 height tableview.
As you have set fixed height for tableview
If you are setting tableview according to navigation bar, means under
Navigation bar, it should be 504 instead of .
If you have only one TableView you can remove all the contraints an
pin that to 0,0,0,0 .
else you have to show a screenshot of your view controller along with constraints
Thanks

Custom UITableViewCell vertical layout constraints causing error

when I run my app on an iPhone I get the following errors. When I run it in the simulator I do not. If I take the -12-| away then the cell's height collapses to something like 30 pixels. And the UI breaks.
Can someone help me and tell me why?
Thanks
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:0x170285ff0 V:|-(12)-[UIImageView:0x1741ec200] (Names: '|':UITableViewCellContentView:0x17419c7d0 )>",
"<NSLayoutConstraint:0x170286040 V:[UIImageView:0x1741ec200(200)]>",
"<NSLayoutConstraint:0x170286090 V:[UIImageView:0x1741ec200]-(12)-| (Names: '|':UITableViewCellContentView:0x17419c7d0 )>",
"<NSLayoutConstraint:0x174881f40 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x17419c7d0(224)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x170286090 V:[UIImageView:0x1741ec200]-(12)-| (Names: '|':UITableViewCellContentView:0x17419c7d0 )>
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.
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
Unable to simultaneously satisfy constraints.
In a custom UITableViewCell I defined the Layout Constraints as follows:
_imgView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-15-[imageView]-15-|" options:0 metrics:nil views:#{ #"imageView": _imgView }]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-12-[imageView(200)]-12-|" options:0 metrics:metrics views:#{ #"imageView" : _imgView }]];
--- EDIT ---
In response to the contentView.bounds suggestion:
In my UITableViewController I implement the following:
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.estimatedRowHeight = 30.0f;
So a zero height, should not be an issue.
The initial height of the cell is likely smaller than the vertical constraints. This creates an initial height conflict until the contentView's frame changes.
You can work around this issue using one of the following approaches:
Increase the cell's height in the storyboard to initially fit the content.
Change the bounds of the cell's contentView to a larger size:
self.contentView.bounds = CGRectMake(0, 0, 99999, 99999);
You'll find more details in the answers to this auto layout question.
Update:
The "Unable to simultaneously satisfy constraints" conflict is between the imageView constraints trying to set the cell's height to 225 points, and the fixed height trying to set the cell height to 224 points.
Your imageView vertical constraints use 224 (12 + 200 + 12) points. The tableView separator uses 1 point. So the cell height needs to be 225 points for all constraints to be met.

Dynamically sized UIWebView in a UITableViewCell with auto layout - constraint violation

I've got a UITableViewCell which contains a UIWebView. The table view cell adjusts it's height depending on the web view contents.
I've got it all working fine, however when the view loads, I get a constraint violation exception in the debugger (the app continues running and functionally works fine, but I'd like to resolve this exception if possible).
How I've got it set up:
The TableView sets the cell height like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
[_topCell layoutIfNeeded];
CGFloat finalHeight = [_topCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return finalHeight + 1;
}
The cell constraints are as follows:
Arbitrary 7px offset from the cell's contentView (top) to the webView
Web view has arbitrary fixed height constraint of 62px (will expand later once content loads)
Arbitrary 8px offset from the webView to the cell's contentView (bottom)
in my viewDidLoad, I tell the webView to go and load a URL, and in the webViewDidFinishLoad, I update the web view height constraint, like this
-(void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
// fittingSize is approx 500
[self.tableView beginUpdates];
// Exceptions happen on the following line setting the constant
_topCell.webViewHeightConstraint.constant = fittingSize.height;
[_topCell layoutSubviews];
[self.tableView endUpdates];
}
The exception looks like this:
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:0x10964b250 V:[webView(62)] (Names: webView:0x109664a00 )>",
"<NSLayoutConstraint:0x109243d30 V:|-(7)-[webView] (Names: webView:0x109664a00, cellContent:0x1092436f0, '|':cellContent:0x1092436f0 )>",
"<NSLayoutConstraint:0x109243f80 V:[webView]-(8)-| (Names: cellContent:0x1092436f0, webView:0x109664a00, '|':cellContent:0x1092436f0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x10967c210 h=--& v=--& V:[cellContent(78)] (Names: cellContent:0x1092436f0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x10964b250 V:[webView(62)] (Names: webView:0x109664a00 )>
This seems a bit weird. It's implied that the constraint which sets the height of the web view is going to be broken, however the web view does get it's height correctly set, and the tableview renders perfectly well.
From my guesses, it looks like the newly increased web view height constraint (it's about 500px after the web view loads) is going to conflict with the <NSAutoresizingMaskLayoutConstraint:0x10967c210 h=--& v=--& V:[cellContent(78)] setting the cell height to 78 (put there by interface builder). This makes sense, however I don't want that cell content to have a fixed height of 78px, I want it to increase it's height, and functionally, it actually does this, just with these exceptions.
I've tried setting _topCell.contentView.translatesAutoresizingMaskIntoConstraints = NO; to attempt to remove the NSAutoresizingMaskLayoutConstraint - this stops the exceptions, but then all the other layout is screwed up and the web view is about 10px high in the middle of the table view for no reason.
I've also tried setting _topCell.contentView.autoresizingMask |= UIViewAutoresizingFlexibleHeight; in the viewDidLoad to hopefully affect the contentView 78px height constraint, but this has no effect
Any help would be much appreciated
An alternative answer, which ends up being far simpler:
Set the priority of the webViewHeight constraint to something other than required. Works well and there are no warnings. I'd recommend going with this :-)
I worked out a solution. It seems like a hack, but it works correctly and produces no errors and warnings.
The trick was to dissociate the web view's layout and height from the contentView.
I did this as follows:
In the tableviewcell, add a new UIView, called containerView
Give it constraints for Top:0 Left:0 Right:0 but no bottom constraint and no height constraint
Put the web view (and any other content) inside this containerView
Add constraints such that the web view is 7px from the top and bottom of containerView, and has a height constraint of 62px
At this point, the containerView isn't connected to the bottom of the table view cell, and so it could over/under flow without auto-layout complaining.
Now, in tableView:heightForRowAtIndexPath:, we calculate the cell height based on the containerView as follows:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
CGFloat finalHeight = [_topCell.containerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return finalHeight + 1;
}
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
All done!
From the Constraint log
The Constraint you are trying to achieve is as below,
In a 78 px cellContentHeight you want to fit a content of 7(top space) + 62 (Web View) + 8 (bottom space). which is 77 (not equal to 78). Either of two option would work,
Try giving top margin as 8 (instead of 7) and make it 78. (V:|-8-[WebView(62)]-8-|)
Attach to the top, give 8 pixel top space (V:|-8-[WebView(62)]) and don't specify 8 px bottom space.
Attach to Bottom, give 8 pixel bottom space (V:[WebView(62)-8-|) and don't specify top space.

iOS Autolayout: Issue with UILabels in a resizing parent view

I've got a view that contains only a UILabel. This label contains multiline text. The parent has a variable width that can be resized with a pan gesture. My problem is that when I do this resizing the UILabel does not recalculate its height such that all of the content is still visible, it simply cuts it off.
I've managed to fix it with a bit of a hack but it is horribly slow to run:
- (void)layoutSubviews {
CGSize labelSize = [self.labelDescription sizeThatFits:CGSizeMake(self.frame.size.width, FLT_MAX)];
if (self.constraintHeight) {
[self removeConstraint:self.constraintHeight];
}
self.constraintHeight = [NSLayoutConstraint constraintWithItem:self.labelDescription attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:labelSize.height];
[self addConstraint:self.constraintHeight];
[super layoutSubviews];
}
Shouldn't this happen automatically with autolayout?
EDIT
The structure of my view is:
UIScrollView
---> UIView
---> UILabel
Here are the constraints on the UIScrollView:
<NSLayoutConstraint:0x120c4860 H:|-(>=32)-[DescriptionView:0x85c81c0] (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c48a0 H:|-(32#900)-[DescriptionView:0x85c81c0] priority:900 (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c48e0 H:[DescriptionView:0x85c81c0(<=576)]>,
<NSLayoutConstraint:0x120c4920 H:[DescriptionView:0x85c81c0]-(>=32)-| (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c4960 H:[DescriptionView:0x85c81c0]-(32#900)-| priority:900 (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x8301450 DescriptionView:0x85c81c0.centerX == UIScrollView:0x85db650.centerX>,
Here are the constraints on the UIView:
<NSLayoutConstraint:0x85c4580 V:|-(0)-[UILabel:0x85bc7b0] (Names: '|':DescriptionView:0x85c81c0 )>,
<NSLayoutConstraint:0x85c45c0 H:|-(0)-[UILabel:0x85bc7b0] (Names: '|':DescriptionView:0x85c81c0 )>,
<NSLayoutConstraint:0x85c9f80 UILabel:0x85bc7b0.trailing == DescriptionView:0x85c81c0.trailing>,
<NSLayoutConstraint:0x85c9fc0 UILabel:0x85bc7b0.centerY == DescriptionView:0x85c81c0.centerY>
The UILabel itself has no constraints on it, aside from pinning it to the edges of its parent
Okay, I finally nailed it. The solution is to set the preferredMaxLayoutWidth in viewDidLayoutSubviews, but only after the first round of layout. You can arrange this simply by dispatching asynchronously back onto the main thread. So:
- (void)viewDidLayoutSubviews {
dispatch_async(dispatch_get_main_queue(), ^{
self.theLabel.preferredMaxLayoutWidth = self.theLabel.bounds.size.width;
});
}
That way, you don't set preferredMaxLayoutWidth until after the label's width has been properly set by its superview-related constraints.
Working example project here:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch23p673selfSizingLabel4/p565p579selfSizingLabel/ViewController.m
EDIT: Another approach! Subclass UILabel and override layoutSubviews:
- (void) layoutSubviews {
[super layoutSubviews];
self.preferredMaxLayoutWidth = self.bounds.size.width;
}
The result is a self-sizing label - it automatically changes its height to accommodate its contents no matter how its width changes (assuming its width is changed by constraints / layout).
I've fixed this issue after raising a bug with Apple. The issue that multiline text requires a two-pass approach to layout and it all relies on the property preferredMaxLayoutWidth
Here is the relevant code that needs to be added to a view controller that contains a multiline label:
- (void)viewWillLayoutSubviews
{
// Clear the preferred max layout width in case the text of the label is a single line taking less width than what would be taken from the constraints of the left and right edges to the label's superview
[self.label setPreferredMaxLayoutWidth:0.];
}
- (void)viewDidLayoutSubviews
{
// Now that you know what the constraints gave you for the label's width, use that for the preferredMaxLayoutWidth—so you get the correct height for the layout
[self.label setPreferredMaxLayoutWidth:[self.label bounds].size.width];
// And then layout again with the label's correct height.
[self.view layoutSubviews];
}
In Xcode 6.1 for iOS 7/8, I was able to get this to work by just setting preferredMaxLayoutWidth in a setter method that's called on my view to display the text for the label. I'm guessing it was set to 0 to begin with. Example below, where self.teachPieceLabel is the label. The label is wired up with constraints alongside other labels in a view in Interface Builder.
- (void)setTeachPieceText:(NSString *)teachPieceText {
self.teachPieceLabel.text = teachPieceText;
[self.teachPieceLabel setPreferredMaxLayoutWidth:[self.teachPieceLabel bounds].size.width];
}

Resources