UISegmentedControl change number of segments programmatically
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UISegmentedControl is often initialized with a fixed set of segments, but it can also be updated at runtime when available options change. The important part is managing insertion, removal, and selected index safely so the control remains consistent with your view model. A good implementation updates segments from one source of truth instead of mutating the control ad hoc from multiple places.
Basic Segment Manipulation APIs
You can insert, remove, and replace segments programmatically.
Useful methods include:
- '
insertSegment' - '
removeSegment' - '
removeAllSegments' - '
setTitle' - '
setImage'
These are enough for both incremental and full rebuild approaches.
Rebuild From a Data Model
For dynamic screens, rebuilding from an array is usually clearer than applying many scattered mutations.
This makes segment count changes deterministic and easy to test.
Handling Selection Correctly
Changing segment count can invalidate the selected index. If you remove the selected segment and do nothing else, the control may end up with no valid selection or an unexpected one.
Safe strategy:
- preserve selection only if index still exists
- otherwise choose a fallback segment or no selection
Always let the data model decide the fallback rather than guessing inside UI event handlers.
Updating in Response to User Choice
Example: a control that changes available filters based on mode.
This kind of update is fine as long as segment state is not split across multiple controllers.
Animation and UX Considerations
Structural changes can be animated, but constant animated rebuilding may feel unstable. Prefer:
- no animation for frequent updates
- animation only when the user can understand why options changed
- stable ordering of segments across updates
Unexpected movement in controls can hurt usability more than it helps.
UIKit Layout and Styling
After changing segments, you may also need to update:
- selected index
- enabled state per segment
- content offset in surrounding scroll view if embedded in headers
Text length also matters. If segment titles vary widely, the control can become cramped. In those cases a custom segmented UI or a collection-based selector may fit better.
Accessibility
Dynamic segment changes should stay accessible. After rebuilding:
- ensure titles are descriptive
- preserve logical ordering
- avoid unnecessary repeated announcements
If content changes significantly, consider whether VoiceOver users need additional context elsewhere on screen.
Debugging Checklist
If segment updates behave strangely:
- verify a single method owns segment rebuilding
- log selected index before and after update
- confirm target-action is not firing recursively
- make sure updates occur on main thread
Most bugs come from state drift rather than from UISegmentedControl itself.
Common Pitfalls
- Mutating segments from multiple places without a shared source of truth.
- Forgetting to fix
selectedSegmentIndexafter segment removal. - Rebuilding segments too frequently and creating unstable UI behavior.
- Using titles so long that dynamic updates break layout clarity.
- Performing UI updates off the main thread.
Summary
- '
UISegmentedControlsupports runtime segment count changes through insert and remove APIs.' - Rebuilding from a data array is usually cleaner than incremental random mutations.
- Selection state must be revalidated whenever segment count changes.
- Keep updates tied to one model-driven method for predictable behavior.
- Favor clarity and stability over unnecessary animation when options change.
Related reading
- UISlider with increments of 5
- UISplitViewController in portrait on iPhone shows detail VC instead of master
- UIStackView Hide View Animation
- UIStackView Hide View Animation
- UIStackView Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?
- UIStackView Unable to simultaneously satisfy constraints on squished hidden views
- UIStatusBarStyle not working in Swift
- UITableView - change section header color
.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.