Why sometimes isCollapsed of UISplitViewController is false on iPhone portrait?

Viewed 703
3 Answers

@Klaas comment is right on. I could only observe the value of isCollapsed be meaningful after the view is properly laid out and added to the view hierarchy with a proper trait collection. In my case moving logic that depended on isCollapsed from viewWillAppear to viewDidAppear did the trick.

For me, isCollapsed was false even after viewDidLayoutSubviews().

I ended up checking if the view controller's traitCollection.horizontalSizeClass was equal to .unspecified, then I know that isCollapsed is not ready to use yet.

You can then watch for changes on the traitCollection. In my case, I needed to know when horizontalSizeClass is .regular, so the following code did it:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    if traitCollection.horizontalSizeClass == .regular {
        ...
    }
}

On iOS 14, I've found it's required to check if the view has been added to a window (self.view.window != nil) before trusting isCollapsed.

Related