1

I'm using a custom TabViewController. I want to change the right bar button item ONLY in CompanyViewController. However, changing the self.navigationItem.rightBarButtonItem doesn't work - the button doesn't show up on the navigation bar. How can I fix this?

CompanyViewController.swift

let filterButton = UIBarButtonItem(title: "Filters Off", style: .plain, target: self, action: #selector(filterButtonTapped))

self.navigationController?.navigationBar.topItem?.rightBarButtonItem = filterButton // Works
self.navigationItem.rightBarButtonItem = filterButton //Doesn't work

Resource

Alex
  • 2,369
  • 3
  • 13
  • 23

1 Answers1

1

Please use this code:

//Create custom UIButton with image.

let filterButton = UIButton(type: .custom)
filterButton.setImage(UIImage(named: "imagename"), for: .normal)
filterButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
filterButton.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)

//Assign that UIButton to UIBarButtonItem
let item1 = UIBarButtonItem(customView: filterButton)

//set UIBarButtonItem to navigationItem
self.navigationItem.setRightBarButtonItems([item1], animated: true)
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
Mahesh kumar
  • 278
  • 4
  • 15