UISegmentedControl

UISegmentedControl displays multiple segments which each function as a button.

Example

UISegmentedControl

Instructions

Create

  1. Open Xcode
  2. Click File
  3. Click New
  4. Click Project...
  5. Under iOS, click Application
  6. Click Single View Application
  7. Click Next
  8. After Product Name, type your product name
  9. After Organization Name, type your organization name or leave blank
  10. After Organization Identifier, type your organization identifier
  11. After Language, select Swift
  12. After Devices, select Universal
  13. Uncheck Use Core Data
  14. Click Next
  15. Select a folder
  16. After Source Control, uncheck Create Git repository on
  17. Click Create

Edit ViewController.swift

  1. Open ViewController.swift
  2. Copy and paste code

Edit Main.storyboard

  1. Open Main.storyboard
  2. Click View
  3. Click Utilities
  4. Click Show Object Library
  5. Drag Segmented Control to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Sent Events, after Value Changed, drag + to View Controller
  10. Click valueChanged:
  11. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  12. Click interfaceBuilderSegmentedControl

Add Image

  1. Click File
  2. Click Add Files to "UISegmentedControl" ...
  3. Select image
  4. After Destination, check Copy items into destination group's folder (if needed)
  5. After Folders, select Create groups for any added folders
  6. After Add to targets, check UISegmentedControl
  7. Click Add

Run

  1. Click Product
  2. Click Run

Code

import UIKit

class ViewController: UIViewController
{
  @IBOutlet weak var interfaceBuilderSegmentedControl: UISegmentedControl!
  
  override func prefersStatusBarHidden() -> Bool
  {
    return true
  }
  override func viewDidLoad()
  {
    super.viewDidLoad()
    
    self.interfaceBuilderSegmentedControl.setTitle("Interface", forSegmentAtIndex: 0)
    self.interfaceBuilderSegmentedControl.setTitle("Builder", forSegmentAtIndex: 1)
    self.view.removeConstraints(self.view.constraints())
    
    let apportionsSegmentWidthsByContentSegmentedControl = UISegmentedControl(items: ["Apportions", "Segment", "Widths", "By", "Content"])
    apportionsSegmentWidthsByContentSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    apportionsSegmentWidthsByContentSegmentedControl.apportionsSegmentWidthsByContent = true
    apportionsSegmentWidthsByContentSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(apportionsSegmentWidthsByContentSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: apportionsSegmentWidthsByContentSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let backgroundImageSegmentedControl = UISegmentedControl(items: ["Background", "Image"])
    backgroundImageSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    backgroundImageSegmentedControl.setBackgroundImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
    backgroundImageSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(backgroundImageSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: backgroundImageSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: apportionsSegmentWidthsByContentSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let contentOffsetSegmentedControl = UISegmentedControl(items: ["Content", "Offset"])
    contentOffsetSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    contentOffsetSegmentedControl.setContentOffset(CGSizeMake(-4, -4), forSegmentAtIndex: 0)
    contentOffsetSegmentedControl.setContentOffset(CGSizeMake(4, 4), forSegmentAtIndex: 1)
    contentOffsetSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(contentOffsetSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: contentOffsetSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: backgroundImageSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let contentPositionAdjustmentSegmentedControl = UISegmentedControl(items: ["Content", "Position", "Adjustment"])
    contentPositionAdjustmentSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    contentPositionAdjustmentSegmentedControl.setContentPositionAdjustment(UIOffsetMake(4, 4), forSegmentType: UISegmentedControlSegment.Any, barMetrics: UIBarMetrics.Default)
    contentPositionAdjustmentSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(contentPositionAdjustmentSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: contentPositionAdjustmentSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentOffsetSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let dividerImageSegmentedControl = UISegmentedControl(items: ["Divider", "Image"])
    dividerImageSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    dividerImageSegmentedControl.setDividerImage(UIImage(named: "HappyFace"), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
    dividerImageSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(dividerImageSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: dividerImageSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentPositionAdjustmentSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let enabledSegmentedControl = UISegmentedControl(items: ["Enabled", "Disabled"])
    enabledSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    enabledSegmentedControl.setEnabled(true, forSegmentAtIndex: 0)
    enabledSegmentedControl.setEnabled(false, forSegmentAtIndex: 1)
    enabledSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(enabledSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: enabledSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: dividerImageSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let insertSegmentWithTitleSegmentedControl = UISegmentedControl(items: ["Insert", "Segment", "With", "Title"])
    insertSegmentWithTitleSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    insertSegmentWithTitleSegmentedControl.insertSegmentWithTitle("Title", atIndex: 3, animated: true)
    insertSegmentWithTitleSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(insertSegmentWithTitleSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: insertSegmentWithTitleSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: enabledSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let insertSegmentWithImageSegmentedControl = UISegmentedControl(items: ["Insert", "Segment", "With", "Image"])
    insertSegmentWithImageSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    insertSegmentWithImageSegmentedControl.insertSegmentWithImage(UIImage(named: "HappyFace")!, atIndex: 3, animated: true)
    insertSegmentWithImageSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(insertSegmentWithImageSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: insertSegmentWithImageSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: insertSegmentWithTitleSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let momentarySegmentedControl = UISegmentedControl(items: ["Momentary", "1"])
    momentarySegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    momentarySegmentedControl.momentary = true
    momentarySegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(momentarySegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: momentarySegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: insertSegmentWithImageSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let removeSegmentAtIndexSegmentedControl = UISegmentedControl(items: ["Remove", "Segment", "At", "Index"])
    removeSegmentAtIndexSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    removeSegmentAtIndexSegmentedControl.removeSegmentAtIndex(3, animated: true)
    removeSegmentAtIndexSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(removeSegmentAtIndexSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: removeSegmentAtIndexSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: momentarySegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let removeAllSegmentsSegmentedControl = UISegmentedControl(items: ["Remove", "All", "Segments"])
    removeAllSegmentsSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    removeAllSegmentsSegmentedControl.removeAllSegments()
    removeAllSegmentsSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(removeAllSegmentsSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: removeAllSegmentsSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: removeSegmentAtIndexSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let selectedSegmentIndexSegmentedControl = UISegmentedControl(items: ["Selected", "Segment", "Index"])
    selectedSegmentIndexSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    selectedSegmentIndexSegmentedControl.selectedSegmentIndex = 0
    selectedSegmentIndexSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(selectedSegmentIndexSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: selectedSegmentIndexSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: removeAllSegmentsSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let tintColorSegmentedControl = UISegmentedControl(items: ["Tint", "Color"])
    tintColorSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    tintColorSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    tintColorSegmentedControl.tintColor = UIColor.redColor()
    self.view.addSubview(tintColorSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: tintColorSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: selectedSegmentIndexSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    let titleSegmentedControl = UISegmentedControl(items: ["0", "1"])
    titleSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    titleSegmentedControl.setTitle("Title", forSegmentAtIndex: 0)
    titleSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(titleSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: titleSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tintColorSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let imageSegmentedControl = UISegmentedControl(items: ["0", "1"])
    imageSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    imageSegmentedControl.setImage(UIImage(named: "HappyFace"), forSegmentAtIndex: 0)
    imageSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(imageSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: imageSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let shadow = NSShadow()
    shadow.shadowColor = UIColor.blackColor()
    shadow.shadowOffset = CGSizeMake(2, 2)
    
    let titleTextAttributesSegmentedControl = UISegmentedControl(items: ["Title", "Text", "Attributes"])
    titleTextAttributesSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    titleTextAttributesSegmentedControl.setTitleTextAttributes(NSDictionary(objectsAndKeys: UIFont(name: "Times New Roman", size: 24)!, NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName, UIColor.greenColor(), NSBackgroundColorAttributeName, shadow, NSShadowAttributeName), forState: UIControlState.Highlighted)
    titleTextAttributesSegmentedControl.setTitleTextAttributes(NSDictionary(objectsAndKeys: UIFont(name: "Times New Roman", size: 24)!, NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName, UIColor.redColor(), NSBackgroundColorAttributeName, shadow, NSShadowAttributeName), forState: UIControlState.Normal)
    titleTextAttributesSegmentedControl.setTitleTextAttributes(NSDictionary(objectsAndKeys: UIFont(name: "Times New Roman", size: 24)!, NSFontAttributeName, UIColor.whiteColor(), NSForegroundColorAttributeName, UIColor.blueColor(), NSBackgroundColorAttributeName, shadow, NSShadowAttributeName), forState: UIControlState.Selected)
    titleTextAttributesSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(titleTextAttributesSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: titleTextAttributesSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imageSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let widthSegmentedControl = UISegmentedControl(items: ["Width", "1"])
    widthSegmentedControl.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    widthSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    widthSegmentedControl.setWidth(200, forSegmentAtIndex: 0)
    widthSegmentedControl.setWidth(20, forSegmentAtIndex: 1)
    self.view.addSubview(widthSegmentedControl)
    self.view.addConstraint(NSLayoutConstraint(item: widthSegmentedControl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleTextAttributesSegmentedControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
  }
  @IBAction func valueChanged(sender: UISegmentedControl!)
  {
    //let alertView = UIAlertView(title: sender.currentTitle, message: nil, delegate: nil, cancelButtonTitle: "Cancel")
    //alertView.show()

    let alertController = UIAlertController(title: sender.titleForSegmentAtIndex(sender.selectedSegmentIndex), message: nil, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
  }
}

External

Apple iOS Developer Library