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.
In the next tab, select Single View Application as a project type.
Write project name and other details.
Once you create a new project, go to Main.storyboard and select ViewController.
Now drag and drop UISegmentedControl from “Show the object library” section.
Now select SegmentedControl and see the attribute inspector which have properties of SegmentedControl. We can manage the segment count, segment titles, image, selection etc.
Now we set outlet UISlider in ViewController as well as add a view for see the segment change event.
@IBOutlet weak var segControl: UISegmentedControl! @IBOutlet weak var vwColorChange: UIView!
Set IBAction for UISegmentedControl.
@IBAction func actionSegmentAction(sender:UISegmentedControl) { switch segControl.selectedSegmentIndex { case 0: vwColorChange.backgroundColor = UIColor.greenColor() case 1: vwColorChange.backgroundColor = UIColor.blueColor() case 2: vwColorChange.backgroundColor = UIColor.magentaColor() default: vwColorChange.backgroundColor = UIColor.greenColor() } }
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.
You may also like,
This page was last edited on November 9th, 2020, at 3:42.