Make a UIBarButtonItem disappear using swift IOS
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Hiding a UIBarButtonItem in iOS is usually done by removing it from the navigation item rather than trying to toggle a hidden property. The safest approach depends on whether the button should disappear temporarily or permanently for a screen state. A clear show and hide pattern keeps navigation behavior predictable.
Remove and Restore Bar Button Items
UIBarButtonItem itself does not provide a direct visibility toggle. Instead, set the corresponding navigation item property to nil, then restore the saved button when needed.
This pattern is simple and works well for permission-based UI changes.
Hide During Async Operations
A common requirement is to disable edit actions while a network request is active. You can remove the button before the request and restore it when done.
Always switch back to the main actor before touching UIKit.
Decide Between Hide and Disable
Sometimes disabling is better than removing. If the action should remain discoverable, keep the button and set isEnabled to false. If the action should be unavailable or misleading in a given state, removing it is cleaner for users.
Use a consistent rule across your app so navigation controls behave the same way on each screen.
Coordinate Visibility with App State
Bar button visibility should be driven by state, not scattered view-controller side effects. A common pattern is to compute visibility from permission state and loading state, then apply it in one method. This avoids inconsistent navigation behavior after repeated screen transitions.
This approach keeps UI decisions deterministic and easy to test. It also prevents cases where one code path removes a button and another path forgets to restore it. In larger apps, state-driven UI updates are far more reliable than one-off imperative updates.
Test Navigation Behavior Across Transitions
After implementing show and hide logic, verify behavior when pushing and popping view controllers. Navigation bars can be reconfigured during transitions, and hidden items may reappear if setup code runs in multiple lifecycle methods.
A practical test plan includes initial load, permission changes, pull-to-refresh flow, and returning from a child screen. Running these checks prevents inconsistent controls in production navigation stacks.
Document the chosen hide-versus-disable rule so all contributors implement the same navigation behavior.
Consistency improves usability across the app.
Common Pitfalls
- Trying to call a hidden property on
UIBarButtonItem, which does not exist. - Modifying navigation items from a background thread.
- Recreating buttons repeatedly and losing target-action wiring.
- Removing a button without preserving a reference for restoration.
Summary
- Hide bar button items by assigning
nilto navigation item slots. - Restore from a saved reference when state changes.
- Use disabling when you want visibility without interaction.
- Keep all UIKit changes on the main thread.

