How to Switch View Controllers Using iOS UISegmentedControl

One of the typical usage of iOS UISegmentedControl is for switching between different switch view controllers. UISegnmentedControl is a horizontal bar that comes with multiple segments. Each of these segments work as a discrete button to display image or text.

In simple words, UISegmentedControl is used to show or hide data based on selected category in the view controller.

UISegmentedControl Example

In our today’s iOS app Tutorial, we’ll demonstrate the implementation process of UISegmentedControl for you to implement in your iPhone app development project.

Let’s Get Started

👉 Open XCode and create new project under file menu.

samplesegment1

👉 In the next tab, select App as a project type.

samplesegment2

👉 Write project name and other details.

samplesegment3

👉 Once you create a new project, go to Main.storyboard and select ViewController.

samplesegment4

👉 Now drag and drop UISegmentedControl from Click on “+” button on Top Right.

samplesegment5

👉 Now select SegmentedControl and see the attribute inspector which have properties of SegmentedControl. We can manage the segment count, segment titles, image, selection etc.

samplesegment7

👉 Now we set outlet UISegmentedControl in ViewController as well as add a view for see the segment change event.

@IBOutlet weak var segControl: UISegmentedControl!
@IBOutlet weak var vwColorChange: UIView!

samplesegment11

👉 Set IBAction for UISegmentedControl.

@IBAction func actionSegmentAction(sender:UISegmentedControl) {
switch segControl.selectedSegmentIndex {
case 0:
vwColorChange.backgroundColor = UIColor.greenColor()
case 1:
vwColorChange.backgroundColor = UIColor.blueColor()
default:
vwColorChange.backgroundColor = UIColor.greenColor()
}
}

ViewController

And done!

Now that you know the implementation process of iOS UISegmentedControl for iPhone app development, you can implement it in any iPhone app to create switch views for different purposes.

However, if you’ve doubts, then you can contact our development team for help.

Grab a free copy of Custom Segmented Control Example demo from Github.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.