Skip to main content

How to Use UISegmentedControl in iOS - Objective C

UISegmentedControl




Objective

Main objective of this blog post is to give you an idea about how to Use UISegmentedControl in iOS.
You will get Final Output: 
select-green



Introduction:
UISegmentedControl is a horizontal bar, which is made up of multiple segments. Each segment works as a discrete button and it can display a title or an image. UISegmentedControl is used when you want to show/ hide data based on the selected category in the same view.
For example, you want to display different lists in the same tableView based on the segment selected.
Let’s create a simple app in which the color of the view changes based on the selected segment from UISegmentedControl.

Step 1 Create Xcode Project

Create a new xCode project and select single view application and name it as SegmentedControlDemo.

Step 2 Add Segmented Control

Now go to Main.Storyboard and add the SegmentedControl in the viewController
segmented-controls


Step 3 Design UI

Make user interface like this and give necessary constraints: 
green-orange-cyan

You can change the number of segments in the control from the attributes inspector. 
attributes-inspector



You can also change the names of segments by using following property from attributes inspector. 
plain

You can also change the tint and background color from attribute inspector: 
background-color

Step 4 Create IBOutlet

Add one UIView below the segmented control. After that make IBOutlets for both segmented control and view. Here we want to change the background color of view based on the selected segment.
To know the current selected segment and based on it to perform the action, make an IBAction for segmented control.
  • @property (weak, nonatomic) IBOutlet UISegmentedControl *segControlForColor;
  • @property (weak, nonatomic) IBOutlet UIView *vwToDisplayColor;
  • - (IBAction)segColorBtnTapped:(id)sender;

Step 5 Create IBAction Method

UISegmentedControl has a property called selectedSegmentIndex, which gives the index of current selected segment.
Write below code in the IBAction method that we created earlier:
  • - (IBAction)segColorBtnTapped:(id)sender {
  • if(segControlForColor.selectedSegmentIndex==0){
  • vwToDisplayColor.backgroundColor=[UIColor greenColor];
  • }
  • else if(segControlForColor.selectedSegmentIndex==1){
  • vwToDisplayColor.backgroundColor=[UIColor orangeColor];
  • }
  • else{
  • vwToDisplayColor.backgroundColor=[UIColor cyanColor];
  • }
  • }


This method changes the color of view based on segment selection.

Step 6 Build & Run the project

Run the project, it will look like this: 
run-the-project

Here, you can see that the color of view is changing based on the selected segment. But suppose you want this type of effect in your segmented control to make it more attractive. 
select-orange



Step 7 Initialized SegmentControl

You may have the question like: How to change the tint color for individual segment?
Write below code in your viewDidLoad method:
  • - (void)viewDidLoad {
  • [super viewDidLoad];
  • //To change the tint color
  • [[segControlForColor.subviews objectAtIndex:0]
  • setTintColor:[UIColor cyanColor]];
  • [[segControlForColor.subviews objectAtIndex:1] setTintColor:[UIColor orangeColor]];
  • [[segControlForColor.subviews objectAtIndex:2] setTintColor:[UIColor greenColor]];
  • }
SegmentedControl has a property called subViews that is an array of all the views associated with the segments. Here, index 0 means back most view in the segmented control.
tintColor is also a property of this control which is used to set the tint color for that particular object from subView array.
I hope you will find this blog post very useful. While working with UISegmentedControl in iOS. Let me know in comment if you have any questions regarding in IOS. I will reply you ASAP

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...