2

I'm using UIStackView for layouting in one of my tableview cell xibs to make resizing easier, and I'm also using size classes as there are layout differences between phones and tablets.

Due to some reason, I have to use a different stackview for each size class. Say stackview1 for iPhones and stackview2 for iPads. Now I have moved label1 into stackview1 in iPhone size class, switched to iPad size class and wanted to move label1 to stackview2 from stackview1(which is greyed out). But once I do this and switch back to iPhone size class, Label1 is moved out of stackview1 and the iPhone layout screws up.

Wondering if anyone has encountered this before and got any solutions?

UPDATED

To clarify further,

  1. I implemented the following two layouts purely via constraints and I was convinced by by colleagues that layout using UIStackView can be easier to adapt to UI changes. For example, in the layouts below, there is a distance constraint (12) between label1 and label3, if I want label3 to move up once label1 and label2 are hidden/nil, I will have to manually set the distance constraint to 0. While if via UIStackView, set label1 and label2 as hidden and we are all set. This is just an example, there are much more changes like this.

  2. The reason why I need different UIStackView for sizes class is because of the design differences.

Phone Layout iPhone Layout Tablet Layout iPad Layout

Shawn
  • 311
  • 2
  • 9
  • This doesn't sound like something you're going to be able to configure using just the "vary for traits" feature in the nib — at least, not the way you describe it. You might have to perform the change in code when the trait collection changes. Or perhaps you could explain why you need a _different_ stack view for each size class? That's a Bad Smell if you ask me. – matt May 20 '18 at 02:38
  • @matt I've updated my question with more info. Hopefully it explains you the reason why I need different stackview for each size class. – Shawn May 20 '18 at 04:45
  • I have a similar issue with my layouts — a different stackview hierarchy at different trait collections. Instead of moving subviews between stack views, I am considering to have duplicates of subviews (in your case labels) in each stackview, and hiding/showing those subviews applicable to the current trait collection. Possibly using `willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)`. – bio Aug 08 '18 at 11:43
  • Update: I ended up using 2 different .xib files, one for compact width and one for regular width. This way it is possible to have completely different layouts, especially when constraints are not enough. – bio Aug 08 '18 at 20:25

1 Answers1

0

It looks like there is no way to achieve the goal using the same stackview. I ended up resolving it using one xib and apply stackview partially. In my sample above, I put label 1, 2 and 3 in a stackview because the layout of these three labels can be reused across different size classes. The rest of the constraints are set individually without stackview.

Shawn
  • 311
  • 2
  • 9