0

I am trying to place an icon on the right of a navigation bar throughout my app. This is everywhere but the home screen (though it would be fine there as well, since I hide it on that view controller): the table view controller at the top, and the map view and table view controllers on the far right of the image below.

This code mysteriously works to set the icon in the center of the navigation bar on one view controller (Favorites view controller, top left), which is a tableview controller, segued from a UIViewController that is embedded in a navigation controller:

let yelpIcon = UIBarButtonItem(image: UIImage(named: "Yelp_trademark_RGB_outline"), style: .plain, target: self, action: nil)
self.navigationController!.navigationItem.rightBarButtonItem  = yelpIcon

enter image description here

My Map and TableViewControllers (far right) are embedded in navigation controllers, that are each embedded in a tab bar controller. I cannot get the image to show up by creating a navigation item in Interface Builder and setting its image, or by doing it programmatically.

enter image description here

This code is recommended highly on SO, but has no result when I try it (I have action set to nil, since I don't need it to do anything):

let button1 = UIBarButtonItem(image: UIImage(named: "myimage"), style: .plain, target: self, action: Selector("action"))
self.navigationItem.rightBarButtonItem = button1

Note: Nav bar is stylized with black barTintColor, which shouldn't make a difference but I thought I'd share. Image is a PNG in my assets folder. In using the debug view hierarchy, I came across these two layers that appear to be blocking my image from appearing (though they don't do this with the back button).

enter image description here

Instance address 0x7fa8b7b10aa0 is a UIVisualEffectSubView. I'm not sure if this is the issue, but I can't figure out how to turn it off either. I have changed the view tint color in the navigation bar on the navigation controller in which it is embedded to transparent, as well as turning off opacity. Neither worked.

How can I get the image on the right of my nav bar to appear?

13
  • Hi, could you specify which iOS you're running this on, and what swift version you're using. Also do tell which of these UIViewControllers do you want the icon to show up. Another is that you shouldn't set it on the self.navigationController's navigationItem but rather in the UIViewController's navigationItem. Commented Feb 28, 2018 at 2:11
  • Simulator version 10, Swift 4, iOS 11.1, XCode 9.2 Commented Feb 28, 2018 at 2:13
  • Did you mean, iPhone X, iOS 11.1 and XCode 9.2 or something? there is no Swift 11.1 Commented Feb 28, 2018 at 2:15
  • 1
    Yeah I immediately corrected that. Please refresh Commented Feb 28, 2018 at 2:15
  • I also edited my initial comment could you answer which I've added? Commented Feb 28, 2018 at 2:20

1 Answer 1

6

Please find below solution.

Create extension of UIViewController like this and method,

extension UIViewController {
    func setNavigationItem() {
        let imageView = UIImageView(image: UIImage(named: "yelp"))
        let item = UIBarButtonItem(customView: imageView)
        self.navigationItem.rightBarButtonItem = item
    }
}

and for those ViewController, in which you want to show rightBarButton Item, in viewDidLoad() method, add following line,

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNavigationItem()
}

Please remove inside UINavigationController of UITabbarController, the Initial UINavigationController will handle all navigation throughout the app.

Let me know in case of any queries.

Sign up to request clarification or add additional context in comments.

7 Comments

Hmmm. Nothing seems to appear for me?
@Pigpocket can you please share your code? and hope each ViewController is child of UINavigationController
Sure, but how exactly? My code is exactly like yours, except for this line, where I needed to call out the type: let item = UIBarButtonItem(image: UIImage(named: "Yelp_trademark_RGB_outline")
can you please debug & confirm that the method called or not? or you are getting item or not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.