Wobbly Stacks

June 3, 2016

Making Stack View Controllers Play Nice

I’m working on build a master / detail application that uses UIStackViewController that gracefully chooses the right layout on iPhone and iPad using size classes, and adapts cleanly to context changes from iPad multitasking.

After I got the basic flow that I wanted, I noticed that UIKit was logging Unbalanced calls to begin/end appearance transitions... the first time I showed a detail view controller in the collapsed state. After consulting the tea leaves (stack overflow) I didn’t see anything that directly applied to what I was doing. After a bit of digging I figured out that at app launch I was setting the stack view’s primary and secondary regardless of if I was launching into the collapsed or expanded state.

I fixed the issue by only setting the primary controller on launch, then later in the setup process checking the relevant trait collections on the window after it’s been made key and visible and making the call as to whether or not I should be showing the detail view controller as well.