Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ViewController's Back Button Image for all screens

I have this below code that changes the back button image on next screen.

I have 30 screens in my app and i want back button to be same across all 30 screens. Is it possible that I don't copy paste this code on all 30 screens and just write it once and rather reuse it across 30 screens.

Also, the code attached with back button should work fine on all screens when i reuse it

I am using iOS 8 and Xcode 6.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backButtonImage: UIImage = UIImage(named: "back")!
    var backBarItem: UIBarButtonItem = UIBarButtonItem(image: backButtonImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))

    segue.destinationViewController.navigationItem.leftBarButtonItem = backBarItem;
}

func method() {
    self.navigationController?.popViewControllerAnimated(true)
}
like image 239
Sategroup Avatar asked Aug 01 '15 08:08

Sategroup


1 Answers

You can change it globally

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back")

Or per navigation controller.

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.backIndicatorImage = UIImage(named: "custom-back")
    navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "custom-back")  
}

Full detail here https://sarunw.com/posts/how-to-change-back-button-image/

like image 198
sarunw Avatar answered Sep 29 '22 14:09

sarunw