How to show image in bar button item as is?

How would I take the tint off of a bar button item so that the image of the button shows as is? With the tint set, blue being the default, it shows the image as that color instead of the actual image.

Accepted Reply

After reading this, I realized there is a MUCH simpler solution.

https://stackoverflow.com/questions/31117069/changing-tab-bar-item-image-and-text-color-ios


- Select the bar button in IB

- Set the image (MyImage from xcAssets) in Bar item field (need to be the right size)

This you had done probably.


Now the key point:

- select MyImage in xcAssets

- Open attributes inspector, look at the top, in Image Set:

- change 'Render As' from 'Default' to 'Original Image'.


That's it.

Replies

I did it that way:


declare an IBOutlet for the button (here the back button).


Then in viewDidLoad:


        let backButtonImage = UIImage(named: "My Image") 
        let backView = UIImageView(image: backButtonImage)
        backView.frame.size = CGSize(width: 100, height: 50)
        backButtonItem.customView = UIView(frame: CGRect(x:0, y:0, width: 100, height: 50))
        backButtonItem.customView?.addSubview(backView)


You would have to restore the action after this. You can add a tapGesture:


        let backButtonImage = UIImage(named: "My Image") 
        let backView = UIImageView(image: backButtonImage)
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapImage(_:)))
        backView.addGestureRecognizer(tapGesture)
        backView.isUserInteractionEnabled = true
        backView.frame.size = CGSize(width: 100, height: 50)
        backButtonItem.customView = UIView(frame: CGRect(x:0, y:0, width: 100, height: 50))
        backButtonItem.customView?.addSubview(backView)



with the action (here, there was an unwindSegue for the back button ; gave it an identifier to reuse programmatically):

    @objc func tapImage(_ recognizer: UITapGestureRecognizer) {
        print("Button Tapped")  // 15.1.2020 Should be a back button
        performSegue(withIdentifier: "unwindToStartViewController", sender: nil)
    }

After reading this, I realized there is a MUCH simpler solution.

https://stackoverflow.com/questions/31117069/changing-tab-bar-item-image-and-text-color-ios


- Select the bar button in IB

- Set the image (MyImage from xcAssets) in Bar item field (need to be the right size)

This you had done probably.


Now the key point:

- select MyImage in xcAssets

- Open attributes inspector, look at the top, in Image Set:

- change 'Render As' from 'Default' to 'Original Image'.


That's it.