Split View Controller

In iOS applications, there are the requirements to split the screen into two sections and display a view controller on each side to show the application content. In this section of the tutorial, we will discuss the Split View Controller, which is an important part of an iOS application.

The Split View Controller is a container View Controller that manages the master-detail interface by splitting the screen into two sections so that the user can interact with the master interface to get the detail in the detail view controller. For example, the settings app in IPad is shown in the master-detail interface, as shown in the following image.

iOS Split View Controller

The Split View Controller is an instance of UISplitViewController, which inherits UIViewController.

A Split View Controller manages two child view controllers in the master-detail interface. The changes in the master view controller drive changes to the detail view controller. Since iOS 8, this class is available for all iOS devices. In the previous versions, it was available for the iPad only.

Adding Split View Controller to interface

To add Split View Controller to the interface, we need to search for it in the object library and drag the result to the storyboard.

iOS Split View Controller

We need to make the split view controller as the root view controller in the iOS application we are building. It doesn't have a significant visual appearance in the application as it only manages the child view controllers, which are shown to the user.

Configuring the appearance of Split View Controller

We can set the display mode of a split view controller to configure its visual appearance. The preferredDisplayMode property of UISplitViewController can be set to configure the display mode. The possible values for display mode are given in the following table.

ModeDescription
Side-by-SideIn this mode, both the child view controllers are displayed simultaneously on the screen where the master view controller is displayed on the left pane of the screen, and the detail view controller is shown on the right pane. The master view controller is shown typically narrower than the detail view controller. We can adjust the master view controller width by using preferredPrimaryColumnWidthFraction property. This mode is represented by UISplitViewController.DisplayMode.allVisible constant.
HiddenIn this mode, the primary(master) view controller is hidden and becomes off-screen, whereas, the detail view controller becomes on screen. To present the primary view controller, we must present it modally or changes the display mode. This mode is represented by UISplitViewController.DisplayMode.primarryHidden constant.
OverlayIn this mode, the primary view controller is layered onto the top of the detail view controller. In this mode, the primary view controller obscures the secondary view controller. This mode is represented by UISplitViewController.DisplayMode.primaryOverlay constant.

There are possibilities that the split view controller doesn't follow the display modes due to the space constraints. The split view controller can-not display the child view controllers side by side in the compact horizontal environment.

UISplitViewController Properties

The UISplitViewController class contains the following properties to customize the split view behavior and manage the child view controllers.

SNPropertyDescription
1var delegate: UISplitViewControllerDelegate?It represents the delegate that is used to receive the split view controller messages.
2protocol UISplitViewControllerDelegateThe protocol UISplitViewControllerDelegate contains the methods that are notified when the changes made to the split view interface.
3var viewControllers: [UIViewController]It is the array of view controllers that is managed by the receiver.
4var presentsWithGesture: BoolIt is a boolean type value that determines whether the hidden view controller can be presented with the swipe gesture.
5var preferredDisplayMode: UISplitViewController.DisplayModeIt is the preferred display mode of the interface.
6var displayMode: UISplitViewController.DisplayModeIt represents the current arrangement of the split view controller's contents.
7var displayModeButtonItem: UIBarButtonItemIt represents the button that changes the display mode of the interface.
8var primaryEdge: UISplitViewController.PrimaryEdgeIt represents the side on which the primary view controller remains.
9var isCollapsed: BoolIt is a boolean value that indicates whether one of the child view controllers is displayed.
10var preferredPrimaryColumnWidthFraction: CGFloatIt represents the relative width of the primary view controller.
11var primaryColumnWidth: CGFloatIt represents the width in points of the primary view controller.
12var minimumPrimaryColumnWidth: CGFloatIt represents the minimum width (in points) required for the primary view controller.
13var maximumPrimaryColumnWidth: CGFloatIt represents the maximum width required for the primary view controller.

UISplitViewController Methods

The UISplitViewController class contains the following action methods to display the child view controllers.

SNMethodDescription
1func showDetailViewController(UIViewController, sender: Any?)It presents the detail view controller of the split view interface.
2func show(UIViewController, sender: Any?)It presents the primary view controller of the split view interface.

Example

In this example, we will create an iOS application that implements the master-detail interface.

Interface Builder

To create the interface builder for the project, first, we need to add the Split view controller to the storyboard. For that purpose, search for a split view controller and drag the result to the storyboard. This will add a split view controller to the interface builder, as shown in the following image.

iOS Split View Controller

The above image contains a master view controller that is a table view controller and a UIViewController to implement a detail view controller. A table view controller will display the list of records where the detail of each record is displayed on the detail view controller.

First, make the split view controller as the initial view controller. Let's start designing the storyboard. First, we will design a prototype table view cell by adding a label. We will also define the auto-layout rules for the label within the content view of the cell, as shown in the following image.

iOS Split View Controller

Now, we will design the detail view controller. We will add the label to the detail view controller to show the content. We will also define the auto-layout rules for the label within the detail view controller, as shown in the following image.

iOS Split View Controller

Now, we will create the subclass of UITableViewController and assign it to the master view controller. We will also create a subclass of UITableViewCell and assign it to the table view cell. Create the connection outlet for cell content label in the TableViewCell. Also, create the subclass of UIViewController to represent the Detail View Controller and connect the outlet of the label in this.

TableViewCell.swift

ViewController.swift

TableViewController.swift

Output:

iOS Split View Controller




Latest Courses