loading

iOS: Segment Control

Segment control is characterized as a horizontal control that manages several segments, each of which is controlled by a different button. Multiple views can be presented within a single view controller by utilizing a segment control, where each view is displayed using a separate button.

The declaration of the segment control is as follows.

				
					class UISegmentControl : UIControl 
				
			
Unless a specified width is given, the UISegmentedControl automatically resizes segments to fit proportionately within their superview. You can ask for fading and sliding effects to be added to the action when you add and delete parts.
 
The interface builder can be enhanced with segment control by following these procedures.
 
1.Navigate to the object library, search for SegmentControl, and then drag the result onto the storyboard.
 
2.SegmentControl can have its appearance customized by creating an outlet.
 
3.Establish a SegmentControl action link to set up the logic for when the value changed event is triggered.
 
4.To control the size and placement of Segment Control on various sized iOS devices, configure the Auto Layout rules for Segment Control.

Example

To demonstrate this, we will incorporate the segment control into our interface builder and utilize it to show the different XIB files that we have generated.

Interface Builder

In this example, the individual views on the occurrence of horizontal segment control are displayed using two XIBs. Segment control, which regulates how the views are displayed appropriately, is a feature of the main storyboard.

Ios: Segment Control -

What is a XIB file?

What is called an XML interface builder is XIB. With the aid of cocoa and carbon, we can create graphical user interfaces thanks to the interface builder. In order to offer the application’s user interface, XIB files are loaded during runtime. The XIB files, which are representations of UIView, are saved as NIB or XIB files.

VC1.xib

The VC.xib file can be seen in the picture below.

Ios: Segment Control -

VC2.xib

The VC2.xib file can be seen in the image below.

Ios: Segment Control -

ViewController.swift

We will establish the action connection for the segment control in the ViewController.swift file. This connection will be alerted each time the valueChanged event for the segment control is triggered.

It switches between the Segment Control valueChanged event and the VC1.xib and VC2.xib.

				
					import UIKit  
  
class ViewController: UIViewController {  
  
  
    @IBOutlet weak var segmentedControl: UISegmentedControl!  
      
    @IBOutlet weak var viewContainer: UIView!  
      
    var views = Array<UIView>()  
      
    override func viewDidLoad() {  
        super.viewDidLoad()  
        // Do any additional setup after loading the view.  
        views.append(VC1().view!)  
        views.append(VC2().view!)  
        for v in views{  
            viewContainer.addSubview(v)  
        }  
        viewContainer.bringSubviewToFront(views[0])  
    }  
      
    @IBAction func switchViewAction(_ sender: UISegmentedControl) {  
        viewContainer.bringSubviewToFront(views[sender.selectedSegmentIndex])  
    }  
}  
				
			

Output

Ios: Segment Control -
Share this Doc

iOS: Segment Control

Or copy link

Explore Topic