0

So i subclassed UITableViewCell and have an appropriate xib file for it.
To keep it simple lets just say i have 3 UILabels side by side.
All of them have a spacing of 8 to each other and are resized to fit its content except the first one, which takes the remaining space in the cell.
The first label (Label1) has trailing space constraints to both other labels and the superview. Those constraints have their Installed property not checked and are connected to my subclass via IBOutlets.
Visually it could look like this:

[Label1] - Spacing - [Label2] - Spacing - [Label3]

Depending on the data displayed, the right 2 UILabels can be hidden. If the first one is hidden it should look like this:

[Label1] - Spacing - [Label3]

Hiding the label is done in tableView:cellForRowAtIndexPath: in which i also call a method to set the appropriate constraint from the xib-file which can be accessed via an IBOutlet. This is working perfectly. But when rotating the device, the constraints are reloaded from the xib-file in which no constraint is Installed, but updateConstraints is not called again on my UITableViewCell-subclass, messing everything up.

One way to fix it would be by, instead of calling the method, which sets the appropriate constraint active, in tableView:cellForRowAtIndexPath: i could call it in layoutSubviews in my UITableViewCell-subclass. But i think you shouldn't update constraints in layoutSubviews.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
manuelwaldner
  • 551
  • 1
  • 5
  • 24
  • are you sure you are giving proper constraints to those labels other then the horizontal spacing which you have mentioned? i mean it also depends on other constraints. And one more important thing is the horizontal content hugging priority. Make sure you are doing this one properly. Also post the screenshot of accurate form of ui and the messed ui. – Mahesh Agrawal Dec 24 '16 at 07:45
  • Sry for the late answer, but i did search a little bit more and found this question http://stackoverflow.com/questions/27663249/activateconstraints-and-deactivateconstraints-not-persisting-after-rotation-fo which is exactly my problem. – manuelwaldner Jan 13 '17 at 11:13
  • if this question problem is same as yours, then you must using size classes. and in this case, if you have set the constraints for the horizontal align while you are switched the IB mode to iphone portrait mode only, then practically for landscape mode there is no constraint present for those. this might be the cause. – Mahesh Agrawal Jan 13 '17 at 11:19
  • I fixed it like the accepted anwser suggested and created those constraints programmatically and switch those. Then they persist after the rotation. – manuelwaldner Jan 13 '17 at 15:32

0 Answers0