Within my scene delegate, I'm creating a navigation controller and attempting to add a right bar button item to all the view controllers. Here's my code within scene delegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: scene)
let viewController = IntroViewController()
let navigation = UINavigationController(rootViewController: viewController)
navigation.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Test", style: .done, target: nil, action: nil)
navigation.navigationItem.rightBarButtonItem?.tintColor = .red
let standardAppearance = UINavigationBarAppearance()
standardAppearance.backgroundColor = .black
navigation.navigationBar.standardAppearance = standardAppearance
navigation.navigationBar.scrollEdgeAppearance = standardAppearance
window.rootViewController = navigation
self.window = window
window.makeKeyAndVisible()
}
Unfortunately, this code doesn't add any bar button item to the navigation controller on any of the view controllers.
However, when I add this line of code to one of my view controllers within viewDidLoad, it'll add the navigation bar button item to only that view controller.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Exit", style: .plain, target: self, action: #selector(exitPressed))
Is there a way to add a bar button item to all the view controllers within a navigation controller programmatically.