Swift
iOS Development
UIKit
UIBarButtonItem
Navigation Bar

UIBarButtonItem in navigation bar programmatically?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

swift
1import UIKit
2
3final class DetailsViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6        title = "Details"
7
8        navigationItem.rightBarButtonItem = UIBarButtonItem(
9            title: "Save",
10            style: .done,
11            target: self,
12            action: #selector(saveTapped)
13        )
14    }
15
16    @objc private func saveTapped() {
17        print("Save tapped")
18    }
19}

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.

swift
1navigationItem.rightBarButtonItem = UIBarButtonItem(
2    barButtonSystemItem: .add,
3    target: self,
4    action: #selector(addTapped)
5)

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.

swift
1let share = UIBarButtonItem(
2    barButtonSystemItem: .action,
3    target: self,
4    action: #selector(shareTapped)
5)
6
7let edit = UIBarButtonItem(
8    title: "Edit",
9    style: .plain,
10    target: self,
11    action: #selector(editTapped)
12)
13
14navigationItem.rightBarButtonItems = [share, edit]

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.

swift
1let button = UIButton(type: .system)
2button.setImage(UIImage(systemName: "bell"), for: .normal)
3button.setTitle(" Alerts", for: .normal)
4button.addTarget(self, action: #selector(alertsTapped), for: .touchUpInside)
5button.sizeToFit()
6
7navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)

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.

swift
let root = DetailsViewController()
let nav = UINavigationController(rootViewController: root)
window?.rootViewController = nav

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 @objc on 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 UIBarButtonItem and attached to navigationItem.
  • 'viewDidLoad is the normal place to configure them.'
  • Prefer built-in system items for standard actions.
  • Use rightBarButtonItems or leftBarButtonItems for multiple actions.
  • Reach for customView only when built-in bar button items are not enough.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.