iOS UI Split View Controller

  Total Views : 96
  Comments : 0
 
  Keywords : ios uisplitview example in swift, how to use ios splitview controller in swift with example, Use ios splitview to show multiple controllers in swift with example

Here we will learn iOS ui split view control in swift with example and how to use iOS uisplitviewcontroller to manage two side by side panes of content in swift with example using xcode.

iOS UI Split View Controller

In iOS split view is used to manage the content of two side by side panes, like showing content in the first pane and related information in the second pane. The iOS split view will show the two panes content based on the size of device, like if the device is in portrait orientation then it will allow you see content in both panes otherwise it will show only one pane at a time.

 

If we use iOS split view in our applications that will be like as shown below

 

ios ui splitview controller sample format

 

We can use Split View in our iOS applications by adding UISPlitViewController class reference.

 

Now we will see how to use iOS UI Split view controller in our iOS applications with example.

Create iOS UI Split View Controller App in Swift

To create new project in iOS Xcode open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In welcome window click on the second option “Create a new Xcode Project” or choose File --> New --> Project

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose template.

 

The new Xcode window will contain several built-in app templates to implement common type of iOS apps like page based apps, tab-based apps, games, table-view apps, etc. These templates are having pre-configured interface and source code files. 

 

For this iOS Split View example we will use most basic template “Single View Application”. To select this one, Go to the iOS section in left side --> select Application --> In main area of dialog select “Single View Application” and then click on next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get window like as shown below, in this we need to mention project name and other details for our application.

 

Product Name: “SplitView”

 

The name whatever we enter in Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all apple devices in case if you have any specific requirement to run app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Use Core Data: Unselected

 

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on Next button like as shown below.

 

Create ios ui splitview controller project in swift

 

Once we click on Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below

 

Give path to save new ios application in xcode

 

After click on Create button the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files which we used to design app user interface and to maintain source code. 

 

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface.

 

ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.

 

Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below.

 

ios ui spliview controller application storyboard file in xcode

 

Now select ViewController.swift file in your project that view will be like as shown below.

 

Viewcontroller file in ios uisplitviewcontroller project in xcode

Add iOS UI Controls to View in Swift

Now we will add controls to our application for that open Object Library. The Object Library will appear at the bottom of Xcode in right side. In case if you don't find Object library, click on the button which is at the third position from the left in the library selector bar like as shown below. (Alternatively you can choose View --> Utilities --> Show Object Library.)

 

Object Library in Xcode Application to Search for UI Controls

 

As we discussed our user interface will be in Main.storyboard file so open Main.storyboard file. Now in Object library search for the Split View in Filter field then drag and drop the Split View into Main.storyboard ViewController like as shown below.

 

Add splitview controller to ios storyboard file in xcode

 

Now we will create a new class for that Go to the File --> New --> File like as shown below

 

Add new class file in ios splitview application using xcode

 

Once we click File new templates section will open in that choose the “Cocoa Touch Class” and then click on next button like as shown below

 

Add new class cocoa touch class in ios splitview controller

 

Give the name of the class “DetailViewController” and extend with subclass “UIViewController” click on next button like as shown below

 

Give name to new class file in ios splitview controller example

 

Go to the Second View Controller click on it and assign the class “DetailViewController” like as shown below

 

Map controller to new class in ios splitview application

 

Now we will create another class for that Go to the File --> New --> File

 

Add new class file in ios splitview application using xcode

 

In new templates section select “Cocoa Touch Class” and then click on next button like as shown below

 

Add new class cocoa touch class in ios splitview controller

 

Give the name of the class “MasterViewController” and extend with subclass “UITableViewController” click on next button alike as shown below.

 

Give name to new class file in ios splitview controller example

 

Once we add new class “Root View Controller” will create in our application. Now click on “Root View Controller” and assign the class “MasterViewController” like as shown below.

 

Map controller to new class in ios splitview application

 

After that click on “Prototype Cells” and give the identifier “cell” like as shown below

 

change prototype in ios splitview controller application

 

Now drag the label from object library and drop into the Detail View Controller like as shown below

 

ios splitview add label controller to view controller in xcode

Connect iOS UI Controls to Code in Swift

Now we will make connection between controls and ViewController.Swift code for that click on assistant button (overlap circle) in Xcode toolbar right side corner like as shown below

 

Assistant Editor in iOS Xcode to Mapp Controls with Code

 

To map the controls, press Ctrl button in keyboard and drag label control from canvas interface and drop into DetailViewController.swift file like as shown below

 ios splitview map label control to viewcontroller code in xcode

 

Now make the connection between Root ViewController and Detail ViewController for that hold Ctrl button in keyboard and press the yellow button in Root ViewController and drop into Detail ViewController and set the “Show” modal.

 

iOS Splitview make connection between controllers

 

Now click on the identifier between Root and Detail ViewController and give the name “SendDataSegue” like as shown below

 

ios splitview change the connection name between controllers in xcode

 

Once we finished our configurations we need to bind data to table view for that write the code in MasterViewController.swift file like as shown below

 

//  MasterViewController.swift

//  SplitView

//  Created by Tutlane on 16/08/2016.

//  Copyright © 2016 Tutlane.com. All rights reserved.

 

import UIKit

class MasterViewController: UITableViewController {

var arraylist = [String]()

override func viewDidLoad() {

super.viewDidLoad()

arraylist = ["iOS","Android","Windows Phone"]

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return arraylist.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

cell.textLabel?.text = arraylist[indexPath.row]

return cell

}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

{

self.performSegueWithIdentifier("SendDataSegue", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "SendDataSegue" {

let indexPaths=self.tableView!.indexPathsForSelectedRows!

let indexPath = indexPaths[0] asNSIndexPath

let vc = segue.destinationViewControlleras! DetailViewController

vc.textt = self.arraylist[indexPath.row]

}

}

}

Now we need to write a code to show item details when click on it for that write code in DetailViewController.swift file like as shown below

 

//  DetailViewController.swift

//  SplitView

//  Created by Tutlane on 16/08/2016.

//  Copyright © 2016 Tutlane.com. All rights reserved.

 

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var SegueLabel: UILabel!

var textt = String()

override func viewDidLoad() {

super.viewDidLoad()

self.SegueLabel.text = self.textt

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Now we will run and check the output of application. To run application, select the required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run ios splitview application in xcode using simulator

Output of iOS Split View App in Swift

Following is the result of iOS Split View application. Here we are using iPhone 6s Plus simulator because of that our will split into two views like as shown below

 

iOS splitview application example result in iphone

 

Now change the simulator iPhone 6s Plus to iPad Pro and run the application our ios split view application result will be like as shown below.

 

ios splitview controller example result in ipad simulator

 

This is how we can use iOS ui splitview controller in swift to show the two side by side pane based on our requirements.

 

PREVIOUS

iOS UI TableView
 

NEXT

iOS UI WebView  
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User