Search code examples
iosuitableviewautolayout

iOS - Autolayout issue in table view cell


I have a table view cell with the following items:

  • Image View (343 x 193)
  • Label 1 (height: 17)
  • Label 2 (height: 20)

I want the image to maintain the same aspect ratio no matter what the device is (this app only works on iPhone, not on iPad). What I'm doing is adding the following:

  • Image View: Aspect Ratio contraint; top, left, right contraints

  • Label 1: Height constraint; top (in respect to image view), left,
    right contraints; equal widths (with image view)

  • Label 2: Height constraint; top (in respect to image view), left,
    right, bottom (in respect to cell) contraints; equal widths (with
    image view)

When I test this I get the following

2018-04-14 11:50:33.457531-0500 MyAppIOS[4357:479920] [LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x60000009f9a0 UIImageView:0x7ff04952dfb0.width == 1.7772*UIImageView:0x7ff04952dfb0.height   (active)>",
    "<NSLayoutConstraint:0x60000009fc70 UILabel:0x7ff04952e1e0.height == 17   (active)>",
    "<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>",
    "<NSLayoutConstraint:0x60000009fea0 V:|-(10)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009fef0 H:[UIImageView:0x7ff04952dfb0]-(16)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff40 H:|-(16)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff90 V:[UIImageView:0x7ff04952dfb0]-(8)-[UILabel:0x7ff04952e1e0]   (active)>",
    "<NSLayoutConstraint:0x600000280140 V:[UILabel:0x7ff04952e1e0]-(8)-[UILabel:0x7ff04952e940'Loading...']   (active)>",
    "<NSLayoutConstraint:0x6000002801e0 V:[UILabel:0x7ff04952e940'Loading...']-(10)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60800009c070 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff04952d770.height == 266   (active)>",
    "<NSLayoutConstraint:0x60800009e230 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ff04952d770.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>

So basically it's saying that it will modify the height of the second label to accommodate.

Is there a better way to set up these constraints? Should I have more/less than the ones I'm using?


Solution

  • The reason behind that conflict is that the cell initially assumes a static height constraint that conflicts withe current constraint setup in the subviews. That's clear from UIView-Encapsulated-Layout-Height

    So set the priority of the bottom constraint of the label2 to 999.

    (Also, if you set the left & right constraints, don't set the width constraint.)