5

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)
}
Sategroup
  • 955
  • 13
  • 29

5 Answers5

2

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/

sarunw
  • 8,036
  • 11
  • 48
  • 84
1

To general change the appearance of UI-Elements in iOS look at UIAppearance. This way you can set it once and it will be everywhere in your app.

I would recommend setting it in the AppDelegate application:didFinishLaunchingWithOptions:.

Try this:

let backImg: UIImage = UIImage(named: "back")!
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)
palme
  • 2,499
  • 2
  • 21
  • 38
1

I only have one navigation Controller in my app, so this may or may not be helpful. But, I created a subclass of UINavigationController. Then in this subclass, I override the pushViewController method:

override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    let pushingVC = viewControllers[viewControllers.count - 1]
    let backItem = UIBarButtonItem()
    backItem.title = ""
    pushingVC.navigationItem.backBarButtonItem = backItem
    super.pushViewController(viewController, animated: animated)
}

This, makes it so every time a viewController is pushed, from my customNavigationController, it uses the custom back button for every view. You have to make sure you change your UINavigationControllers type to your custom Subclass. But, this works for me in Swift 3.0.

Daniel Jones
  • 1,032
  • 10
  • 18
1

You could use appearance()

Swift

    let backImage = UIImage(named: "back")
    UINavigationBar.appearance().backIndicatorImage = backImage
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
        UIColor.white], for: .normal)
    UIBarButtonItem.appearance().tintColor = UIColor.green
0

I'm not quite sure what you wanted, so I'll answer most of what I think you could want for your sake and anyone looking at this in the future.

First: You want a back button similar to those on default apple apps. To do so, you need to a) get a reference to the destination scene and it's view controller. I will assume you have done so and set it equal to controller for future reference. b) You need to set the left bar button item. Set it with:

controller.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Insert Title Here!!", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

Place all of this in prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) (you will also find the destination scene with this, likely with segue.destinationViewController

Second: You want to use an image named "Back" for all items. If so, repeat step a) above, and insert this handy bit of code:

UIBarButtonItem(image: "Back", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

I also know you want to not have to copy it, but it's not as if this is that computer intensive anyways and it's probably simplest.

Third: You're smart and don't hate advances in programming so you want to use a storyboard. If so, simply drag in a Navigation Bar from the object menu, add a navigation item, and copy it to all of your scenes. If your scenes are linked to custom classes, then super happy fun time good news, you can simply link the navigation item to your custom class through an IBAction and use that function to do all the fancy coding your heart could ever desire.

Fourth: You really, really don't want to copy anything even if it's not that hard. If so, you can subclass a UIViewController with your own custom class, add the code I mentioned above, and then subclass all your future UIView classes as this. To be honest, I'm not entirely sure this would work and it seems unnecessarily laborious, but it's up to you.

I'm a bit tired right now so sorry for all the things I'm finding funny, but I hope I helped, and please tell me if I missed your point entirely and I can try and think of a solution.

dunnmifflsys
  • 613
  • 2
  • 9
  • 21