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
To add a UIBarButtonItem programmatically, you usually create it in a UIViewController and assign it to navigationItem.leftBarButtonItem or navigationItem.rightBarButtonItem. The important requirement is that the view controller must be inside a UINavigationController, otherwise there is no navigation bar to display the item.
Add a basic bar button item
The classic target-action pattern is still the standard approach:
If this controller is pushed onto a navigation stack, the Add button appears on the right side of the navigation bar. The @objc action method is required because the selector uses Objective-C runtime messaging under the hood.
Use a text button or image button
You are not limited to system items. You can create a button with a title:
Or an SF Symbols image:
This is often the cleanest choice when the action should match platform conventions such as share, edit, close, or compose.
Add multiple buttons
If you need more than one item on a side, use the array properties:
The order of the array controls the order in the navigation bar. This is useful for screens that expose a primary action and a secondary utility action side by side.
Use a custom view when standard items are not enough
If the built-in styles are too limited, UIBarButtonItem can host a custom view:
This gives you more control over layout and styling, but it also means you take on more responsibility for sizing, accessibility, and platform consistency. Use it when standard bar button items genuinely cannot express what you need.
Make sure the navigation controller exists
A very common source of confusion is correct code that still shows no button. In most cases, the issue is not the UIBarButtonItem itself; it is that the view controller is presented modally without a navigation controller.
For example:
Wrapping the controller in UINavigationController gives it a navigation bar, so the item becomes visible.
Common Pitfalls
The most common mistake is setting navigationItem.rightBarButtonItem on a controller that is not embedded in a navigation controller. The code runs, but nothing appears.
Another common issue is forgetting the @objc action method or using the wrong selector name. In that case, the button may exist but tapping it will fail.
People also overuse customView when a standard item would be better. Standard items integrate more naturally with system appearance, spacing, and accessibility.
Finally, if you configure the item before viewDidLoad or later overwrite navigationItem properties elsewhere, the bar button can disappear unexpectedly.
Summary
- Create
UIBarButtonIteminstances and assign them to the view controller'snavigationItem. - Use system items, titles, or SF Symbols depending on the action you want.
- Use
rightBarButtonItemsorleftBarButtonItemsfor multiple buttons. - Wrap the controller in
UINavigationControllerif there is no navigation bar yet. - Prefer standard bar button items over custom views unless you need custom behavior or styling.
Related reading
- 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
- UIButton inside a view that has a UITapGestureRecognizer
.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.