UIBarButtonItem in navigation bar programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a UIBarButtonItem in code is the normal UIKit approach when a screen’s navigation actions depend on runtime state. The important detail is that the item belongs to the current view controller’s navigationItem, not to the navigation bar globally.
Add a Simple Bar Button Item
The most common pattern is to create a button in viewDidLoad and assign it to the view controller’s navigation item.
This is the right default for actions such as save, edit, or share when the button is owned by the screen itself.
Prefer System Items When Possible
If UIKit already provides a matching system item, use it instead of inventing custom text for a standard action.
System items match the platform better and often avoid localization work.
Multiple Bar Button Items
If the screen needs several actions, use the array properties.
That gives you predictable ownership and keeps action handling within the view controller.
Custom Views Are the Exception
If built-in items are not enough, you can wrap a custom control in a bar button item.
This works, but it also means you are now responsible for sizing, accessibility, and visual consistency. Use customView when you need it, not by default.
Make Sure the Controller Is Embedded Correctly
If the bar button never appears, the first thing to check is whether the controller is inside a UINavigationController.
Without a navigation controller, the navigation item exists conceptually, but there is no visible navigation bar to render it.
Updating Items Later
If the enabled state, title, or style changes based on app state, keep a reference and update it instead of constantly rebuilding the entire navigation setup.
That makes your controller code easier to maintain and keeps the UI changes local.
This also helps with testing and screen-state restoration, because one stored item reference is easier to reason about than repeatedly recreating navigation bar configuration from scattered code paths.
That small structural choice often keeps UIKit controller code noticeably cleaner.
It also makes future UI changes less error-prone.
Common Pitfalls
- Setting the item on the navigation bar instead of on the view controller’s
navigationItem. - Forgetting
@objcon selector-based action methods. - Expecting a bar button item to appear when the controller is not embedded in a navigation controller.
- Using a custom view when a system item would provide better platform consistency.
- Recreating bar button items repeatedly when updating an existing item would be simpler.
Summary
- Programmatic navigation buttons are created with
UIBarButtonItemand attached tonavigationItem. - '
viewDidLoadis the normal place to configure them.' - Prefer built-in system items for standard actions.
- Use
rightBarButtonItemsorleftBarButtonItemsfor multiple actions. - Reach for
customViewonly when built-in bar button items are not enough.
Related reading
- UIBarButtonItem in navigation bar programmatically?
- UIBarButtonItem target-action not working?
- UIBarButtonItem with custom image and no border
- UIButton custom font vertical alignment
- UIButton doesn't listen to content mode setting?
- UIButton events. What's the difference?
- UIButton how to center an image and a text using imageEdgeInsets and titleEdgeInsets?
- UIButton Image Text IOS
.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.