Search code examples
iosswiftuistoryboardsize-classes

Controller's view with different layouts in cell using size classes


I have a custom controller who's view I add to two different cells.

The frame of one cell is wider than higher (kind of landscape) and the other cell is higher than wider (portrait).

My controller is designed in storyboard using different size classes to be able to show different layouts when I push the controller (works like charm).

I hoped adding this controller's view to the two cell's contentView would give me the same functionality, unfortunately it does not :(

How can I achieve that the layout gets adapted the same way as when I push the controller?


Solution

  • What I end up doing was adding the mentioned controller to its parent controller (the one that holds the cells) and then change the TraitCollection with the help of setOverrideTraitCollection(:forChild:). In my case it was changing the verticalSizeClass like this:

    setOverrideTraitCollection(UITraitCollection(verticalSizeClass: .compact), forChild: viewController)
    addChild(landscapeAudioPlayerController)