How do I use a UISegmentedControl to switch views?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
To switch views with a UISegmentedControl in iOS, add a target action that responds to the .valueChanged event, then show/hide child views (or swap child view controllers) based on the selectedSegmentIndex. The segmented control acts as a tab-like selector — each segment maps to a different content view. The two main approaches are toggling view visibility (simple) and using container view controllers (scalable for complex content).
Basic Setup with View Toggling
When the user taps a segment, segmentChanged hides all views except the one matching the selected index.
Container View Controller Approach
For complex content, swap child view controllers instead of simple views:
This properly manages the view controller lifecycle — each child VC receives viewWillAppear/viewDidDisappear events.
SwiftUI Implementation
In SwiftUI, Picker with .segmented style replaces UISegmentedControl. The @State variable triggers a view update when the selection changes.
Customizing Appearance
With Navigation Bar
Placing the segmented control in navigationItem.titleView is a common iOS pattern (used in Apple's own apps like Mail and Files).
Common Pitfalls
- Not calling
didMove(toParent:): When using child view controllers, you must calladdChild()before adding the view anddidMove(toParent: self)after. Skipping this breaks the view controller lifecycle —viewWillAppearandviewDidAppearwill not be called. - Memory from keeping all views loaded: The simple toggle approach loads all views at once. For heavy content (images, web views, maps), this wastes memory. Use the container VC approach and optionally release off-screen VCs.
- Missing
.valueChangedevent: Adding the target with the wrong event (e.g.,.touchUpInside) means the action never fires.UISegmentedControlfires.valueChanged, not touch events. - Forgetting
selectedSegmentIndex = 0: The default selected index is-1(none selected). If you do not set an initial selection, no segment appears selected and no content is shown until the user taps. - Animation when switching: Abruptly showing/hiding views feels jarring. Add a
UIView.transitionorUIView.animatefor smooth crossfade:UIView.transition(with: containerView, duration: 0.2, options: .transitionCrossDissolve, animations: { ... }).
Summary
- Handle the
.valueChangedevent onUISegmentedControlto detect selection changes - For simple content, toggle
isHiddenon child views based onselectedSegmentIndex - For complex content, use container view controllers with proper child VC lifecycle management
- In SwiftUI, use
Pickerwith.segmentedstyle bound to a@Statevariable - Place the segmented control in
navigationItem.titleViewfor navigation-bar integration - Always set
selectedSegmentIndexto a valid value inviewDidLoad
Related reading
- How do I use custom keys with Swift 4's Decodable protocol?
- How do I use databinding to combine a string from resources with a dynamic variable in XML?
- How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?
- How do I use Safe Area Layout programmatically?
- How do I use Safe Area Layout programmatically?
- How do I use subscript and superscript in Swift?
- How do I use toolsoverridelibrary in a build.gradle file?
- how do I use UIScrollView in Interface Builder?
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.