2 Replies
      Latest reply on Jan 15, 2020 1:29 PM by Claude31
      ShinehahGnolaum Level 1 Level 1 (0 points)

        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.

        • Re: How to show image in bar button item as is?
          Claude31 Level 8 Level 8 (8,455 points)

          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)
              }