UISegmentedControl
Description
UISegmentedControl displays multiple segments which each function as a button.
Example
Instructions
Create
- Open Xcode
- Click File
- Click New
- Click Project...
- Under iOS, click Application
- Click Single View Application
- Click Next
- After Product Name, type your product name
- After Organization Name, type your organization name or leave blank
- After Organization Identifier, type your organization identifier
- After Language, select Swift
- After Devices, select Universal
- Uncheck Use Core Data
- Click Next
- Select a folder
- After Source Control, uncheck Create Git repository on
- Click Create
Edit ViewController.swift
- Open ViewController.swift
- Copy and paste code
Edit Main.storyboard
- Open Main.storyboard
- Click View
- Click Utilities
- Click Show Object Library
- Drag Segmented Control to View
- Click View
- Click Utilities
- Click Show Connections Inspector
- Under Sent Events, after Value Changed, drag + to View Controller
- Click valueChanged:
- Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
- Click interfaceBuilderSegmentedControl
Add Image
- Click File
- Click Add Files to "UISegmentedControl" ...
- Select image
- After Destination, check Copy items into destination group's folder (if needed)
- After Folders, select Create groups for any added folders
- After Add to targets, check UISegmentedControl
- Click Add
Run
- Click Product
- 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)
}
}