How do I switch UISegmentedControl programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To switch a UISegmentedControl segment programmatically in Swift, set its selectedSegmentIndex property. Setting this property changes the visual selection but does not fire the .valueChanged action by default. If you need the value-changed handler to execute, call sendActions(for: .valueChanged) after setting the index. In SwiftUI, bind a @State variable to a Picker with .segmented style and update the variable to switch segments.
UIKit: Setting selectedSegmentIndex
Triggering the Value Changed Action
Setting selectedSegmentIndex does not fire .valueChanged. To trigger the handler:
Or call your handler directly:
Deselecting All Segments
Set selectedSegmentIndex to -1 to deselect all segments:
This is useful when the segmented control should start with no selection, requiring the user to make an explicit choice.
Setting Up from Storyboard
Dynamic Segments
Add, remove, and modify segments at runtime:
Customizing Appearance
SwiftUI: Picker with Segmented Style
Using an Enum for Type Safety
Common Pitfalls
- Assuming
selectedSegmentIndextriggers.valueChanged: Setting the index programmatically does not fire the action. CallsendActions(for: .valueChanged)or invoke your handler directly if you need the associated logic to run. - Using an out-of-range index: Setting
selectedSegmentIndexto an index beyond the number of segments crashes with an out-of-bounds exception. Always validate the index:if index < segmentedControl.numberOfSegments. - Forgetting
UISegmentedControl.noSegmentfor deselection: Setting the index to an arbitrary negative number may not work on all iOS versions. UseUISegmentedControl.noSegment(which equals-1) for cross-version safety. - Not updating content when switching programmatically: When a user taps a segment,
.valueChangedfires and your handler runs. When you switch programmatically, you must manually trigger the content update since the handler does not fire automatically. - Using wrong Picker style in SwiftUI:
Pickerdefaults to a wheel or menu style. You must explicitly add.pickerStyle(.segmented)to get the segmented control appearance.
Summary
- Set
segmentedControl.selectedSegmentIndex = nto switch segments programmatically in UIKit - Call
sendActions(for: .valueChanged)after setting the index if you need the action handler to fire - Use
UISegmentedControl.noSegment(-1) to deselect all segments - In SwiftUI, use
Pickerwith.pickerStyle(.segmented)and bind to a@Statevariable - Use an enum with
CaseIterablein SwiftUI for type-safe segment values
Related reading
- How do I take a full screen Screenshot in Swift?
- How do I take a full screen Screenshot in Swift?
- How Do I Take a Screen Shot of a UIView?
- How do I test if a string is empty in Objective-C?
- How do I upload a build to iTunes Connect for TestFlight?
- How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView
- How do I use a UISegmentedControl to switch views?
- How do I use custom keys with Swift 4's Decodable protocol?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.