15

I have UIViewController 1 , that has scroll view. Inside this scrollview there is container view that pinned to top/bottom leading/trailing (without fixed height). Container view has UITableView pinned to top/bottom trailing/leading and height constraint with 0 constant, that will change in updateViewConstraints to content size height.

When View of UIViewController 1 appears, Container View has constraint:

NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)], NSLayoutConstraint:0x7b0ba120 V:|-(0)-[UITableView:0x7c42a200] (Names: '|':UIView:0x7b0b7000 ), NSLayoutConstraint:0x7b0ba1b0 V:[UITableView:0x7c42a200]-(0)-| (Names: '|':UIView:0x7b0b7000 ), NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0)]

Will attempt to recover by breaking constraint

NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)]

What is UIView-Encapsulated-Layout-Height? How can i skip it?Because it breaks "right" constraint(that i update to content size height).Thanks

vien vu
  • 4,277
  • 2
  • 17
  • 30
Serd
  • 421
  • 1
  • 4
  • 13
  • Think it's the height of your tableview cell. How are you setting that? – beyowulf Dec 01 '15 at 02:30
  • I didn't have any constraints on cell. Cell height determine by heightForRowAtIndexPath: , that returns tableView.rowHeight. How is it possible?Thanks – Serd Dec 01 '15 at 08:49
  • What shows up when you build and run? Have you tried changing the background color of your views so you can see how they're being drawn? – beyowulf Dec 01 '15 at 12:44
  • Shown container view with zero height. But i want to make it the size, based on content view, but NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)] was dropped, because this constraint NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0)] – Serd Dec 01 '15 at 13:58
  • How can make container view have a height based on content views(content views has right constraints for Width/Height i'm sure)? – Serd Dec 01 '15 at 13:59
  • "Container view has UITableView pinned to top/bottom trailing/leading and height constraint with 0 constant" What are the constant values for top/bottom/trailing/leading? Don't you need to update one of those when you change the height constant? You've pinned it in such a way that it will dictate a certain size (height/width). Now you want to change the height, so you should also have to change the pinning. – beyowulf Dec 01 '15 at 14:56
  • top, bottom, left, right has 0 constant;Height constant is equal to 0 too. Container view has top, bottom, left, right constraint with 0 constant too. In runtime i'm changing height constant of tableView, so the content is increasing height and i want to container view to increase it height too. How can i do it?Thanks – Serd Dec 01 '15 at 15:30
  • When you set those constraints did you get a red warning or anything? Or is everything scrollview, container view, tableview all have height equal 0? If you set up the constraints correctly, changing the tableview's height will have the desired effect. – beyowulf Dec 01 '15 at 16:00
  • I have a red warning in scroll view, because container view don't have height constraints. TableView height constant is 0. But in runtime IB add this NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0)] that kills my tableview constraint. So changes to table view height constraint would not affect anything. Do you have some ideas?Thanks – Serd Dec 01 '15 at 16:03

7 Answers7

31

Finally i solve this problem.
1. In your tableView setting, set: tableView.estimatedRowHeight = 100.
2. In the last view of your cell, set: make.bottom.equalTo(contentView).priority(999).
3. Run your code, maybe it's ok!

小良俨
  • 319
  • 3
  • 4
18

Finally i found a problem. View that is added as subview to container view has translatesAutoresizingMaskIntoConstraints = YES, so NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0)] was appeared and made me some problems. Add runtime attribute translatesAutoresizingMaskIntoConstraints = NO and it'll fix the issue.

Serd
  • 421
  • 1
  • 4
  • 13
  • This solved my problem! After the container view is added. Use the prepareForSegue method to get to the view,then set to false the translatesAutoresizingMaskIntoConstraints of that view – João Nunes Dec 11 '15 at 12:12
  • it also can be set in interface builder in runtime attributes – Serd Dec 11 '15 at 15:41
  • Thank you! I was adding a child view controller (not using container views, explicitly in code) and I had forgotten to set the child VC's view's translatesAutoresizingMaskIntoConstraints property to false – Protongun Aug 02 '16 at 12:40
11

Chiming in with one of the easiest ways to solve this: Just lower the priority of the autolayout rule that is bound to the bottom edge of your container view.

Here's my container:

enter image description here

I'm telling that image view to follow the bottom edge of the container, and that was giving me a conflict. To fix it, I can just lower the priority of that bottom edge rule:

enter image description here

I say "bottom" because in my estimation that's probably the one you'll have a conflict with, but it could be any edge that implicitly sets a width on your container.

SimplGy
  • 20,079
  • 15
  • 107
  • 144
2

Rather than manually setting translatesAutoresizingMaskIntoConstraints on the contentView of the cell or manually adjusting the height of this view, you should just set estimatedRowHeight and rowHeight for the tableview, as discussed in WWDC 2014 video What's New in Table and Collection Views:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

Then the fully qualified constraints you apply to that cell will adjust the row height and eliminate those annoying warnings.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

Go on the item's container's xib -> deselect "Autoresize Subviews" under the "drawing" section. It fixed it for me. (The item is the item that's giving you the constraints conflict)

drawing

Giulio Colleluori
  • 1,261
  • 2
  • 10
  • 16
0

These issues occurs when there is a clash between the constraint.

In your case, you have already given top/bottom leading/trailing constraint to inner table view,which satisfy required constraint for table view, so there is no need of providing one more constraint (fixed height constraint with constant 0) to it.

NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)], NSLayoutConstraint:0x7b0ba120 V:|-(0)-[UITableView:0x7c42a200]

above two line means there is some clash in vertical direction for table view which has constant 0.

Two ways you can resolve this,

1 - Remove height constraint, set bottom constraint to zero initailly. Take an outlet of bottom constraint and set bottom constraint value dynamically.

2 - Remove bottom space constraint, set height constraint to zero. Take an outlet of height constraint and set height of table view dynamically.

Hope this will help you.

Vikas Mishra
  • 246
  • 1
  • 12
-1

Set the container view's height constraint. Create an IBOutlet of said height constraint call it "containerViewHeightConstraint" or something. When you update the table view's height constraint to 54, say something like:

self.containerViewHeightConstraint.constant = 54.0;
beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • Thanks for answer, but containerViewHeightConstraint and TableViewConstraint are in different view controllers. How can i know in view controller 1 ,when tableview constraint changed?Thanks – Serd Dec 02 '15 at 08:53
  • You can use delegation or NSNotificationCenter – beyowulf Dec 02 '15 at 12:30
  • It's bad approach, i hope(and sure) that there is better solution.Nevertheless Thanks – Serd Dec 02 '15 at 12:36
  • Really the bad approach is using child view controllers. You should avoid that practice. But I tried to tell you, what you're saying makes no sense. You're pinning everything to the edges then setting the height to zero, you must be getting warnings or errors from auto layout. You should investigate those. – beyowulf Dec 02 '15 at 12:59
  • I only have working from scroll view, where container is pinned, because scroll view can not determine it's content size. But what i want - is that container view has height base on the content it's represent. Thanks – Serd Dec 02 '15 at 13:32
  • The main problem for me is this constraint - NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0) – Serd Dec 02 '15 at 13:34