UISplitViewController in portrait on iPhone shows detail VC instead of master
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding UISplitViewController Behavior in Portrait Mode on iPhone
When working with `UISplitViewController` in iOS development, developers often encounter certain behaviors depending on the device orientation and size class. Specifically, while working in portrait mode on an iPhone, `UISplitViewController` shows the detail view controller instead of the master. This behavior is by design and follows Apple's Human Interface Guidelines for multi-panel interfaces. In this article, we'll examine this behavior, explain its reason, and explore potential solutions and workarounds if a different behavior is desired.
What is UISplitViewController?
`UISplitViewController` is a container view controller used to manage a master-detail interface. It was originally designed for iPad but has gained support for iPhone with the introduction of adaptable user interfaces in iOS 8. A typical use case is a list of items (master) which you can select to view more details (detail).
Behavior in Portrait Mode
Default iPhone Behavior
When `UISplitViewController` is used on an iPhone:
- In landscape orientation, both master and detail view controllers are displayed side-by-side.
- In portrait orientation, iOS, by default, shows only the detail view controller. The master view controller can be accessed with a swipe gesture, a "hamburger" menu, or programmatically toggled by the app.
Design Reasons
The design intent behind this behavior is to provide a more immersive experience for the detail content without the confined space of the master list. Given the smaller screen real estate on iPhones, it ensures that important content isn't cramped.
Handling Display Behaviors
Enforcing a Specific Display Mode
If your application requires showing the master view controller first even in portrait:
- You might decide on using the `preferredDisplayMode` property on `UISplitViewController`. By default, this is set to `.automatic`.
- You could set this to `.oneBesideSecondary`, which encourages both to appear side-by-side in larger size classes but still relies on the default behavior.
- If a side-by-side display is needed in portrait mode as often used in iPad, custom logic could mimic the split view with additional navigation controllers.
- To improve user experience, especially where frequent switching is needed, app-level buttons could facilitate toggling between master and detail views.
Related reading
- UIStackView Hide View Animation
- UIStackView Hide View Animation
- UIStackView Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?
- UIStackView Unable to simultaneously satisfy constraints on squished hidden views
- UIStatusBarStyle not working in Swift
- UITableView - change section header color
- UITableView - scroll to the top
- UITableView Add content offset at top
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.