Constraints show different behaviour on different iPhone screen sizes

I'm on macOS 10.13.6, Xcode 10.1, and am working on an iPhone (only) app.


I've a settings scene (in a tab bar controller) that's rather simple: contains a table view with one prototype cell.

This cell contains a label on the left and a switch on the right.

I want the label to auto-resize according to it's content. Therefore I set both, the "Row Height" and the "Estimate" settings of the table view to "Automatic" and the Lines attribute of the label to 0 (zero) and the Line Break attribute to "Word Wrap". (Additionally did this in code, too, when the cell get's created in cellForRowAt:indexPath.)


I added four constraints to the label:

  • trailing, top and bottom space should be fix in relation to the content view
  • the trailing constraint is a fixed value in relation to the leading of the switch.

The switch has two contraints (and the one above in common with the label):

  • it should be centered vertically
  • its trailing space whould be fixed in regards to the content view.


See screenshot: https://www.dropbox.com/s/qdbmewqnn0gaklz/ConstraintsProblemRedacted.png?dl=0


This is what happens: https://www.dropbox.com/s/v37bxsrdtgf6j3b/ConstraintsProblemOn3SimulatorsRedacted.jpg?dl=0


So, the autoresizing of the table view cells works and the auto-multiline of the label works, too, that's not the problem.

However, I have two questions:

  1. What circumstances can cause the trailing space contraints of the switch cause to not be obeyed?
  2. How can a single prototype cell behave in different ways when it's instanciated two times? Sometimes the switch is too far right, sometimes not.


Obviously, the label text sometimes (but not always, depending on screen size?!) get too long and pushes the switch to the right.


Any help here much appreciated!

Accepted Reply

So, the autoresizing of the table view cells works and the auto-multiline of the label works, too, that's not the problem.

However, I have two questions:

  1. What circumstances can cause the trailing space contraints of the switch cause to not be obeyed?
  2. How can a single prototype cell behave in different ways when it's instanciated two times? Sometimes the switch is too far right, sometimes not.

For 1, if there are conflicting constraints.

For 2, I guess it could be due to some approximative computation of text size, at some stage of the drawing process, with bad effect when it is close to the limit of the constraint.


To fix it, I would first either:

- remove the switch leading constraint and repace (if IB complains) by a constraint on the switch width

- or reduce the priority of this constraint to 900 for instance, leaving the trailing for switch to 1000.


Maybe, if the problem still occurs:

- define a constraint for the label width in IB

- adapt in code, depending on the screen size or tableView width.

Replies

So, the autoresizing of the table view cells works and the auto-multiline of the label works, too, that's not the problem.

However, I have two questions:

  1. What circumstances can cause the trailing space contraints of the switch cause to not be obeyed?
  2. How can a single prototype cell behave in different ways when it's instanciated two times? Sometimes the switch is too far right, sometimes not.

For 1, if there are conflicting constraints.

For 2, I guess it could be due to some approximative computation of text size, at some stage of the drawing process, with bad effect when it is close to the limit of the constraint.


To fix it, I would first either:

- remove the switch leading constraint and repace (if IB complains) by a constraint on the switch width

- or reduce the priority of this constraint to 900 for instance, leaving the trailing for switch to 1000.


Maybe, if the problem still occurs:

- define a constraint for the label width in IB

- adapt in code, depending on the screen size or tableView width.

Thanks so much, @Claude31! You answer inspired me to work on this problem again.

I can't exactly tell what the problem really was and what the solution was but it works now.

I experimented with the priority first, but to no avail.

Then I deleted the switch leading / label trailing constraint and IB came up with a warning – and two possible solutions. I chose the first one and now it works with a switch.leading >= label.trailing constraint. :-)


On to the next dialog! Wish me luck! ;-)