Changing orientation in UITableViewCell

In a IOS App, I have an UITableViewCell with is own Nib and I want this UITableViewCell to react to orientation (In fact I want also this UITableViewCell to react to device: IPhone and IPad)

Suppose there are two labels in my UITableViewCell. In a portrait orientation, the second label is under the first one, in a Landscape orientation the two labels are on the same line.

First I noticed that in interface builder, if the layout of all the elements are Autoresizing Mask (and not inferred) I'm unable to put a constraint on width and heigth of the UITableViewCell and it's contentView.

Now I choose in IB: orientation Portrait, I click on Vary For Traits and I put the necessary constraints. After that, I do the same for orientation Landscape.

Then, whatever the orientation is in IB, I only see my last choice (landscape). IB doesnt react to Portrait or landscape orientation.

And it is the same when I run the App: I only see the landscape layout.

Where is my mistake? Do I have to make differnt nib files for all devices variations?
Answered by Claude31 in 651008022
Did you set variations of constraints for label relative positions ?

But for what you want (adapt to device), the best may be:
  • create IBOutlets for the constraints

  • in viewWillLayoutSubviews(), adapt the value of the constraints

Note that you would have to create both constraints for horizontal and vertical spacing, and one of them would be set to zero to get the needed alignment.
Accepted Answer
Did you set variations of constraints for label relative positions ?

But for what you want (adapt to device), the best may be:
  • create IBOutlets for the constraints

  • in viewWillLayoutSubviews(), adapt the value of the constraints

Note that you would have to create both constraints for horizontal and vertical spacing, and one of them would be set to zero to get the needed alignment.
Yes of course I set variations of constraints

But I don't see the result in Interface Builder.
Imagine just a UItableViewCell.
If I made variations, in portrait mode the width will be for instance 400, and in Landscape Mode the width will be 800.
But there is no visual in IB. If I go in portrait mode, or in landscape mode, the visual doesn't change, even if I put the width in constraint
I tested on a very simple case.
Xcode 12.2

I have xib for a cell.
Inside, a single label.
  • I fix its width by constraint.

  • I create a leading constraint to its parent, with the hR variation (disable the universal case on the first checkbox)

  • I create a centerX constraint to its parent, with hC variation (disable the universal case on the first checkbox)

When I select the Portrait (Orientation buttons at the bottom of the panel, below "View as: iPhone 11 (wC hR)" I get the label on the left
When I select landscape, I get the label at center.

So everything is OK. No need to create several nibs (would be an error to do so).

Could you detail very precisely what you do ?
The point is that if the UIElements of my cell have layout inferred, I can see in IB all the elements placed correctly. But when the program is running, none of the constrains applied.

In the other case, if the UIElements have layout autoresizing make, if I change from portrait to landscape the elements are not placed correctly
If you want to send a demo project by mail, I'll look at it.
You will have to check how you defined constraints and their variations.

Let's explain a bit more.
Imagine you have a constraint that need to be 50 in Portrait and 80 in landscape.

Create the constraint.
  • Then in Size inspector, select the object and double click on the constraint

  • Check the value is 50 or set it.

  • Create a variation, with h Regular and w Any

  • Then deselect the "generic" constraint and enable the variation


Create the constraint a second time
  • Open it

  • Set its value to 80

  • Create a variation, with h Compact and w Any

  • Then deselect the "generic" constraint and enable the variation


Now, the layout will change when tou select the presentation Portrait / Landscape in Xcode or when you rotate the device of course.
Changing orientation in UITableViewCell
 
 
Q