How to add a right button to a UINavigationController?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In iOS, right navigation bar buttons are attached to a view controller’s navigationItem, not directly to the UINavigationController. This distinction is important when button actions seem not to appear or stop responding. Once configured correctly, right bar buttons are reliable entry points for common actions such as add, save, and filter.
Core Sections
Basic Right Button in a View Controller
In UIKit, set rightBarButtonItem in viewDidLoad:
This works when the controller is inside a navigation stack.
Push Controller Into Navigation Stack
If no navigation bar appears, ensure the controller is embedded in UINavigationController:
Without this, navigationItem exists but the bar is not visible on screen.
Custom Text and Icon Buttons
You can create custom-labeled buttons:
Or icon button with SF Symbols:
Use symbols that match app design language and accessibility expectations.
Multiple Right Buttons
For more than one action, use rightBarButtonItems:
Order in array controls visual placement.
Enable, Disable, and Update State
Buttons should reflect current screen state.
Updating button state prevents invalid actions and improves UX clarity.
Closure-Based UIAction for Modern iOS
On newer iOS versions, you can use UIAction for concise setup:
This removes selector boilerplate in simple cases.
Storyboard and Interface Builder Option
If using storyboard, you can add a bar button item to navigation item visually and connect an IBAction. Code and storyboard approaches can coexist, but avoid setting conflicting buttons in both places unless intentional.
Common Action Patterns
Right buttons often trigger:
- presenting modal screens
- pushing detail creation view
- committing form data
- toggling list filters
Keep action names specific and side effects predictable.
Accessibility and Localization
Set accessibility labels for icon-only buttons:
For text buttons, use localized strings rather than hardcoded English so UI remains accessible to all target locales.
Debugging Non-Working Buttons
If button is visible but tap does nothing:
- verify selector method has
@objc - verify method signature has no unexpected parameters
- verify target is alive and not deallocated
- check gesture recognizers are not intercepting touches
Most failures are selector wiring mistakes.
Common Pitfalls
- Setting the button on
UINavigationControllerinstead of the active view controllernavigationItem. - Forgetting to embed screen in navigation controller and expecting bar items to appear.
- Using selector without
@objc, causing runtime action lookup failure. - Defining both storyboard and code buttons unintentionally and overriding expected UI.
- Ignoring accessibility labels for icon-only bar buttons.
Summary
- Add right bar buttons through the current view controller
navigationItem. - Ensure the controller is inside a
UINavigationControllerstack. - Use system items, custom titles, or SF Symbols based on action clarity.
- Keep button state synchronized with screen state and data validity.
- Validate selector wiring and accessibility for production-ready behavior.

