Katz
Katz

Reputation: 866

How change the back button to a custom image in Swift2

Hi,

I'm trying to figure out how to change the back button to a custom image on all ViewControllers. I read though a few questions regarding to this solution, but it was mostly outdated.
I want to change < Back to a stock image I have

enter image description here

I was able to disable to hide the back but that's not my goal my goal is to change the default image to a custom one, I added the custom image to the images.xcassets and that didn't work also I tried to set the System Item to Custom and under Bar Item I selected my image which is image.png, but still didn't work, can someone help me approach this solution? Thanks.

 override func viewDidLoad() {
        
            let image: UIImage = UIImage(named: "images")!
        
    self.navigationController?.navigationItem.backBarButtonItem?.image = image
        
    }
    override func viewDidAppear(animated: Bool) {
//        let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
//        navigationItem.leftBarButtonItem = backButton
        self.navigationItem.setHidesBackButton(false, animated: false)
        
    }

Upvotes: 1

Views: 2607

Answers (2)

Ankita Shah
Ankita Shah

Reputation: 2248

You can add this in AppDelegate

UIBarButtonItem.appearance().setBackButtonBackgroundImage(UIImage(named: name), forState: .Normal, barMetrics: .Default)

This will set back button through out the app

Upvotes: 0

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

Instead of doing that you can create your own custom button and assign it to leftBarButtonItem. Try this out

var backBtn = UIBarButtonItem()
let image: UIImage = UIImage(named: "images")!
backBtn.image = image
//set other properties for your button
//set traget and action
self.navigationItem.leftBarButtonItem = backBtn

You can just pop the current view controller in action of the back button.

Upvotes: 1

Related Questions