Segmented Control In Xamarin.iOS

In this article, we are going to create a Xamarin iOS application that contains UI Segmented control that let the user select the value between the certain range of values. We will create a simple application that contains the set of values and a user can select values among the available values. 

Prerequisites

  1. Knowledge of C#
  2. Basics of Xamarin.

Implementation

  1. Open Visual Studio Community.
  2. Select iOS single view app.
  3. Ener the app name and click on "Next" button.
  4. Enter the project name and solution name, and create the application.

Open Main.Storyboard.

Xamarin

Add the Segmented Control from the toolbox to the View Controller.

Xamarin 

You can increase the number of segments from its properties. Let us suppose, by default, it is 2 and we make it 3.

You can set the title from the Title property.

Xamarin 

Now, you have 3 segments with title 'First', 'Second', and 'Third'.

You can also add the segments from the code. For this, you must have set the Name property of this View. Set the name property to 'MySegmentedControl'.

MySegmentedControl.InsertSegment("Fourth", 3, true);

The first argument is Title; the second one is Position that starts from 0; and the third parameter defines if it is animated or not.

Xamarin

Now, your View will look like something like it. The fourth segment will be added at runtime.

Now, we need to add an event to the Segmented Control.

Select Control and go to Events.

Xamarin

Xamarin  

Go to the Value Changed event, and write the event name. Eg: SegmenteContol_ValueChanged and press Enter.

Xamarin

Select the position where you want to place the event.

Xamarin

Now, write the code inside this event.

Xamarin

Your viewController.cs file will be as follows.

  1. using System;  
  2. using UIKit;  
  3.   
  4. namespace SegmentedControl  
  5. {  
  6.     public partial class ViewController : UIViewController  
  7.     {  
  8.         protected ViewController(IntPtr handle) : base(handle)  
  9.         {  
  10.             // Note: this .ctor should not contain any initialization logic.  
  11.         }  
  12.   
  13.         public override void ViewDidLoad()  
  14.         {  
  15.             base.ViewDidLoad();  
  16.   
  17.             MySegmentedControl.InsertSegment("Fourth", 3, true);  
  18.         }  
  19.   
  20.         public override void DidReceiveMemoryWarning()  
  21.         {  
  22.             base.DidReceiveMemoryWarning();  
  23.             // Release any cached data, images, etc that aren't in use.  
  24.         }  
  25.   
  26.         partial void SegmenteControl_ValueChanged(UISegmentedControl sender)  
  27.         {  
  28.             var index = MySegmentedControl.SelectedSegment;  
  29.             if(index == 0)  
  30.             {  
  31.                 MyLabel.Text = "first";  
  32.             }  
  33.             else if(index == 1)  
  34.             {  
  35.                 MyLabel.Text = "second";  
  36.             }  
  37.             else if(index == 2)  
  38.             {  
  39.                 MyLabel.Text = "third";  
  40.             }  
  41.             else if(index == 3)  
  42.             {  
  43.                 MyLabel.Text = "fourth";  
  44.             }  
  45.         }  
  46.     }  
  47. }   

Note - You have to add the label “MyLabel” to the View so as to change its value according to the selected segment. You can also change the Background color of the View according to your wish.

Build and run the application.

Xamarin   
 
Click on the second segment.
 
 Xamarin
 
Click on the first segment. 

Xamarin

In this way, you can create the application with Segment Control View that is beneficial for selecting the values from the bundle or range of values and perform the necessary action by defining its event accordingly.


Similar Articles