iOS UI Navigation Bar (UINavigationController)

  Total Views : 91
  Comments : 0
 
  Keywords : iOS navigation bar example in swift, How to use ios navigation controller in swift with example, iOS use uinavigationcontroller in view with example, iOS navigation controller with example

Here we will learn ios ui navigation bars or ui navigation controller in swift with example and how to use ios navigation bars or uinavigationcontroller in swift applications with example.

iOS UI Navigation Bar (UI Navigation Controller)

In iOS navigaiton bar or navigation controller is used to arrange the application content in efficient manner. Generally, the navigation bar will appear at the top of an app screen and just below the status bar and it contains button options to enable navigation through a series of hierarchical app screens like as shown below.

 

iOS UI Navigation Controller Sample Example in Swift

 

If we click an option in navigation bar and move to new screen means it will show back button, with a title of previous screen and right button like as shown below and the best way to use navigation bar in our applications is with a navigation controller.

 

iOS Navigation Controller or Bar Sample Example

 

We can use navigation bar in our iOS applications by adding UINavigationBar class reference.

 

Now we will see how to use iOS UI navigation bar in our iOS applications using swift with example.

Create iOS Navigation Bar App in Swift

To create new project in iOS 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 Navigation Bar 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: “iPhone Articles”

 

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.

 

ios create new scroll view app in swift using xcode editor

 

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 the 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 scroll view app storyboard file in xcode

 

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

 

ios scroll view app viewcontroller file in xcode

Add iOS UI Controls to View in Xcode

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 View Controller in Filter field then drag and drop the View Controller into Main.storyboard ViewController like as shown below same way add another View Controller in our Main.storyboard file.

 

Once we done with adding controllers our Main.storyboard file will contain three View Controllers (1 Default Controller + 2 New Controllers) like as shown below

 

ios navigation controller add controllers to storyboard in xcode

 

Now we will add navigation controller in our application for that click on first ViewController --> Go to the Editor --> Embed In --> Navigation Controller like as shown below.

 

Embed Navigation Controller to ViewController in iOS Swift App

 

Now drag the Bar button item from Object library and drop into first ViewController and click on Bar Button Item and change the name of buttons like as shown below

 

ios navigation bar or controller add bar button item in viewcontroller

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 the buttons from first view controller and drop into ViewController.swift file like as shown below

 

iOS Navigation Bar or Controller Map Controls to Swift file in xcode

 

Once we finished mapping buttons to ViewController.swift file now drag the label from Object library and drop into the second ViewController and click on label control & change text like as shown below.

 

ios navigation bar or controller add label controls to view in xcode

 

Same way drags the label from Object library and drop into the third ViewController and change the text of label control like as shown below.

 

ios navigation bar or controller add label control to third view controller

 

Once we done with adding all the controls our Main.storyboard file will be like as shown below.

 

ios navigation bar or controller storyboard file after adding controls in xcode

 

Now we will make connection between buttons in first view controller to second & third view controllers for that press Ctrl button in keyboard and drag the Settings button from first view controller and drop into Setting Screen ViewController like as shown below.

 

ios navigation bar or controller map settings viewcontroller in xcode

 

Once we map now click on connection circle and mention Segue Identifier as “SegueSettings” like as shown below.

 

ios navigation bar or controller change settings connection name

 

Now press Ctrl button in keyboard and drag the Help button from first viewcontroller and drop into Help Screen ViewController like as shown below.

 

ios navigation bar or controller map help button to help controller in swift

 

Now click on the connection circle and give identifier name as “segueHelp”.

 

ios navigation bar or controller change mapping connection name

 

Once we done all the settings we need to write custom code in help & settings button click to navigate to respective controller when click on buttons. Our ViewController.swift file should contain code like as shown below

  

//  ViewController.swift

//  UINavigationBar

// Created By Tutlane on 10/08/2016

// Copyright © 2016 Tutlane. All rights reserved.

 

import UIKit

class ViewController: UIViewController {

// action - help button clicked

@IBAction func helpButtonAction(sender: UIBarButtonItem) {

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

}

// action - settings button clicked

@IBAction func settingsButtonAction(sender: UIBarButtonItem) {

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

}

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

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

// set title for help screen

if segue.identifier == "segueHelp" {

let destVc = segue.destinationViewControlleras! HelpViewController

destVc.navigationItem.title = "Help"

}

// set title for Settings screen

if segue.identifier == "segueSettings" {

let destVc = segue.destinationViewControlleras! SettingsViewController

destVc.navigationItem.title = "Settings"

}

}

}

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 scroll view application using xcode editor

Output of iOS Navigation Bar App in Swift

Following is the result of iOS navigation bar or controller application in swift. Now click on help button, we will move to next controller and our output will be like as shown below.

 

ios navigation bar or controller app result or output

 

This is how we can use iOS ui navigation controller or bar to manage navigation of content based on our requirement in swift applications. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User