2
votes

I need to implement a news feed like instagram but I'm facing some troubles with my constraints.

I have a tableView cell with: imageView, label 1, label 2, view 1 and view 2 in a vertical order like this:

enter image description here

I need to achieve the following conditions:
- my image is square.
- label 1 (the black one) have 5 lines max
- label 2 (the red one) don't have a max number of lines
- view 1 is 30px height
- view 2 is 35px height

My constraints are:

Image:

  • trailing space to superview
  • leading space to superview
  • top space to superview
  • botton space to label 1 with constant=8
  • height = 320 with priority=999 (during runtime I change this value to view's width)
  • Content hugging - Vertical = 751, Horizontal=751
  • Content compression - Vertical = 1000, Horizontal = 751

Label 1:

  • trailing space to superview with constant=-20 and priority=999
  • leading space to superview with constant=20
  • height >=20 with priority= 750
  • bottom space to label2 with constant=8
  • Content hugging - Vertical = 1000, Horizontal=752
  • Content compression - Vertical = 1000, Horizontal = 751

Label 2:

  • trailing space to superview with constant=-20 and priority=999
  • leading space to superview with constant=20
  • height >=20 with priority= 750
  • bottom space to view1 with constant=8
  • Content hugging - Vertical = 1000, Horizontal=752
  • Content compression - Vertical = 1000, Horizontal = 751

View 1:

  • trailing space to superview
  • leading space to superview
  • botton space to view2
  • height = 30
  • Content hugging - Vertical = 1000, Horizontal=250
  • Content compression - Vertical = 1000, Horizontal = 750

View 2:

  • trailing space to superview
  • leading space to superview
  • botton space to superview
  • height = 35
  • Content hugging - Vertical = 1000, Horizontal=750
  • Content compression - Vertical = 1000, Horizontal = 750

During runtime I receive several warning about constraints. It can't satisfy all my constraints.

"<NSLayoutConstraint:0x7fec35712d60 V:[UIView:0x7fec357391e0(30)]>",
"<NSLayoutConstraint:0x7fec3570a8f0 V:[UIView:0x7fec357432a0(35)]>",
"<NSLayoutConstraint:0x7fec350d17b0 V:|-(0)-[UIImageView:0x7fec35711340]   (Names: '|':UITableViewCellContentView:0x7fec35744f30 )>",
"<NSLayoutConstraint:0x7fec350bf830 V:[UIImageView:0x7fec35711340]-(8)-[TimelineLabel:0x7fec3570a980'Usura']>",
"<NSLayoutConstraint:0x7fec350bc7e0 V:[TimelineLabel:0x7fec3570a980'Usura']-(8)-[UILabel:0x7fec350ddb40'Produtos: BATOM: MAC, SOM...']>",
"<NSLayoutConstraint:0x7fec35701f20 V:[UILabel:0x7fec350ddb40'Produtos: BATOM: MAC, SOM...']-(8)-[UIView:0x7fec357391e0]>",
"<NSLayoutConstraint:0x7fec350e90c0 V:[UIView:0x7fec357391e0]-(0)-[UIView:0x7fec357432a0]>",
"<NSLayoutConstraint:0x7fec350e9110 UIView:0x7fec357432a0.bottom == UITableViewCellContentView:0x7fec35744f30.bottom>",
"<NSLayoutConstraint:0x7fec357085c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fec35744f30(0)]>"  
Will attempt to recover by breaking constraint 
NSLayoutConstraint:0x7fec3570a8f0 V:[UIView:0x7fec357432a0(35)];


For each cell in my tableView break a different constant.

I tried a lot of solutions but none works.

Help! Please!

1
You're going to have a hard time finding people who want to make sense of all the constraints you've set up. Honestly, the best thing you can do would be to remove ALL of your constraints and start over adding them in to one object at a time, run the app, and then see if you get any warnings. Autolayout is just one of those things that makes more sense the more you mess with it.Adama
I already did this several times. But the problem occurs when I put all the constraints together.Bruna Garcia
if you can provide a sample project with the constraints setup then it would be easier to run it so the issue could be resolved.darren102

1 Answers

2
votes

The warnings are about unbreakable constraints in a dynamic TableViewCell. But as your layout changes in execution time, you must let your view adapt itself without strict "pixel accuracy" constraints.

You need to set down some of your 1000 priority constraints to 999. This will make Xcode happy about some cases that breaking constraints are needed. In most cases, however, all your constraints will be satisfied. =D