How do I show/hide a UIBarButtonItem?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIBarButtonItem does not have a direct hidden property, so show and hide behavior is achieved by adding or removing items from navigation or toolbar arrays. The best approach depends on whether you need to preserve layout spacing or simply remove the action temporarily. This guide covers clean, maintainable patterns in Swift.
Show and Hide in a Navigation Bar
For a single right item, assign the property when showing and set it to nil when hiding.
This is the simplest and most common pattern.
Managing Multiple Bar Items
If you have multiple actions, store the original item array and restore when needed.
Keeping a saved copy avoids rebuilding button instances repeatedly.
Preserving Layout with Placeholder Items
Sometimes removing an item causes surrounding title or alignment shifts. In those cases, swap with a disabled placeholder item of similar width.
This technique can keep visual balance during temporary state changes.
Toolbar Item Visibility
For toolbars, use setItems and animate transitions.
Keep toolbar state in one place so updates remain predictable.
State-Driven Visibility
In production apps, button visibility should reflect screen state, permissions, or async task status. Centralize this in one render method:
State-driven rendering prevents scattered UI mutations and race conditions.
Navigation State Coordination
Bar button visibility often depends on navigation state, edit mode, and user permissions at the same time. Keep these rules in one state renderer instead of toggling items from many callbacks. Centralized rendering makes transitions easier to reason about and prevents flicker during rapid updates.
For animated transitions, update navigation items inside lifecycle-safe points such as viewWillAppear or explicit state-change handlers on the main thread. This improves consistency during push and pop animations.
Common Pitfalls
A common mistake is creating new button instances every time visibility changes. Repeated creation can lose target-action setup consistency.
Another issue is hiding visually but leaving action paths reachable through other triggers. Ensure UI state and business state stay aligned.
Developers also forget accessibility updates. When actions disappear, verify voice navigation order and announcements still make sense.
Finally, bar item updates must happen on the main thread. Background-thread mutations can lead to intermittent UI issues.
Summary
- Hide or show
UIBarButtonItemby replacing navigation or toolbar item arrays. - Use nil assignment for simple single-item visibility toggling.
- Preserve layout with placeholders only when visual shifts matter.
- Keep visibility logic state-driven and centralized.
- Update bar items on the main thread and verify accessibility behavior.
Related reading
- How do I shuffle an array in Swift?
- How do I size a UITextView to its content?
- How do I solve the INSTALL_FAILED_DEXOPT error?
- How do I sort an NSMutableArray with custom objects in it?
- How do I sort an NSMutableArray with custom objects in it?
- How do I specify that a non-generic Swift type should comply to a protocol?
- How do I start my app when the phone starts on Android?
- How do I switch UISegmentedControl programmatically?
.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.