4

Based on this SO post I updated my TableViewControllers to always call layoutIfNeeded after populating my custom tableviewcells. In fact this is the only way to have the cells render correctly with multiple multiline labels.

The cells now render correct but my console is flooded with broken constraint messages which I can't figure to fix.

For example:

(
"<NSLayoutConstraint:0x167c1f20 UILabel:0x166bf820'Intendant des Wolfgang Bo...'.leading == UITableViewCellContentView:0x167b6de0.leadingMargin>",
"<NSLayoutConstraint:0x167c44b0 UILabel:0x166bf820'Intendant des Wolfgang Bo...'.trailing == UITableViewCellContentView:0x167b6de0.trailingMargin>",
"<NSLayoutConstraint:0x167c0af0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x167b6de0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x167c44b0 UILabel:0x166bf820'Intendant des Wolfgang
Bo...'.trailing == UITableViewCellContentView:0x167b6de0.trailingMargin>

One of the troublesome cells looks as simple as this: CustomTableViewCell

The constraints and also hugging priority / compression resistance seemed to be set properly:

Constraints on ContentView

I tried everything from implementing init:coder/awakeFromNib/layoutSubviews, etc. in my CustomCell and set the autoresizing to UIViewAutoresizing.FlexibleWidth and UIViewAutoresizing.FlexibleHeight also setting the contentView.frame = self.bounds as described in several posts on SO, such as this one but nothing seems to work. Also playing around with >= constraints on the "broken" ones doesn't help.

This is another example in case it helps someone to find a quick solution:

(
    "<NSLayoutConstraint:0x1624f800 H:[UILabel:0x16143b60'27. April 2015 \U00b7 spiegell...']-(8)-|   (Names: '|':UITableViewCellContentView:0x1616f1b0 )>",
    "<NSLayoutConstraint:0x14f022e0 H:|-(8)-[UILabel:0x16143b60'27. April 2015 \U00b7 spiegell...']   (Names: '|':UITableViewCellContentView:0x1616f1b0 )>",
    "<NSLayoutConstraint:0x14d95bf0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x1616f1b0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1624f800 H:[UILabel:0x16143b60'27. April 2015 · spiegell...']-(8)-|   (Names: '|':UITableViewCellContentView:0x1616f1b0 )>

Honestly, what's wrong?

Community
  • 1
  • 1
t0day
  • 121
  • 8
  • Do you set "width" , "trailing" and "leading" constraints for one item? – Okhan Okbay May 04 '15 at 21:01
  • For both of them. Sorry if that wasn't clear enough. So the date label has top, leading, trailing and vertical spacing to the textlbl and textlbl has also leading and trailing but bottom instead of top. – t0day May 04 '15 at 21:03
  • Also do you set one of them or both of them to have a static width? – Okhan Okbay May 04 '15 at 21:04
  • No I didn't. Why would I do that? The only "static" value is the cell height, which is being set automatically if resizing to anything other than 44 in Storyboard. – t0day May 04 '15 at 21:17
  • By saying that "static width" I meant setting width constraint.Log says that it breaks trailingMargin constraint because the constraints you set are not satisfying. – Okhan Okbay May 04 '15 at 21:21
  • I understand what it says but I don't get the actual problem. Both of the labels have trailing and leading constraints to the container margin, so what can be wrong about that? – t0day May 04 '15 at 21:24
  • If you set "width", "trailing margin" and "leading margin" constraints at the same time, then in runtime the "trailing margin" constraint will be broken to satisfy the constraints.It can't be expandable("trailing margin"+"leading margin") and also can't expandable("width" constraint) at the same time – Okhan Okbay May 04 '15 at 21:27
  • But there's no width constraint ?? Only trailing and leading and I also tried changing these to the container instead of the container margin but that didn't help either.. – t0day May 04 '15 at 21:33
  • I'm not sure but try using [Datebl setTranslatesAutoResizingMaskIntoConstraints:YES] and [Textbl setTranslatesAutoResizingMaskIntoConstraints:YES] after layoutIfNeeded – Okhan Okbay May 04 '15 at 21:39
  • How are you creating the cells? Using `dequeueReusableCellWithIdentifier:` or `dequeReusableCellWithIdentifier:forIndexPath:`? It should be the last one, especialy when you are using auto layout. You are probably already using this one, but worth checking. – Rory McKinnel May 04 '15 at 22:26
  • Also you might need to set the UILabel `preferredMaxLayoutWidth` property. I have found that layout can be problematic for multi line labels unless this is set. – Rory McKinnel May 04 '15 at 22:28
  • Both suggestions doesn't work :/ I can build a sample project if you want? – t0day May 06 '15 at 19:07

1 Answers1

-2

You're not very clear, but if the problem that you're trying to fix is getting rid of the warnings in the console, then that can be fixed by changing the priority of the label constraint that the Autolayout is breaking to 999.

pteofil
  • 4,133
  • 17
  • 27
  • I'm sorry but this is not the solution I'm looking for since it's just circumventing the actual issue. I want to find out why the constraints are breaking in order to properly layout the views and fix the issue. Your solution is hacky and doesn't really help.. – t0day May 06 '15 at 19:09
  • :) It's not hacky at all. It's the solution posted here on SO to several questions related to this. Here http://stackoverflow.com/a/25795758/4815066 for example, and read some of the comments too. – pteofil May 06 '15 at 19:38