Change ViewController Back Button Image for all screens

I have this code below that reverses the image of a button on the next screen.

I have 30 screens in my application and I want the button to be the same on all 30 screens. Is it possible that I do not copy this code to all 30 screens and just write it once and rather reuse it on 30 screens.

In addition, the code attached using the "Back" button should work fine on all screens when reused

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) } 
+4
source share
3 answers

To change the look of UI elements in iOS, see UIAppearance . That way you can install it once and it will be everywhere in your application.

I would recommend setting it to AppDelegate application:didFinishLaunchingWithOptions:

Try the following:

 let backImg: UIImage = UIImage(named: "back")! UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default) 
+1
source

There is only one navigation controller in my application, so this may or may not be useful. 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 that every time the viewController is clicked, from my customNavigationController it uses a custom return button for each view. You must make sure that you change the type of UINavigationControllers to your own subclass. But this works for me in Swift 3.0.

+1
source

I'm not quite sure what you wanted, so I will answer most of what I think you might want for your sake, and anyone who looks at it in the future.

Firstly:. You need a back button, similar to the default Apple app button. To do this, you need to: a) get a link to the destination scene and view the controller. Suppose you did this and set it to controller for future reference. b) You need to set the left button element. Install it with:

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

Put it all in prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) (You will also find a destination scene with this, probably with segue.destinationViewController

Second: You want to use the image with the name "Back" for all elements. If so, repeat step a) above and insert this convenient bit of code:

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

I also know that you do not want to copy it, but it is not as if this computer was intense anyway, and it is probably the easiest.

Third: You are smart and do not hate progress in programming, so you want to use a storyboard. If so, just drag the navigation bar from the object’s menu, add a navigation element and copy it to all your scenes. If your scenes are related to custom classes, then super funny fun interesting news, you can simply associate a navigation item with your custom class using IBAction and use this function to do whatever the fantasy can bring you.

Fourth: You really, really do not want to copy something, even if it is not so difficult. If so, you can subclass the UIViewController with your own class, add the code mentioned above, and then subclass all of your future UIView classes like this. Honestly, I'm not entirely sure that this will work, and it seems unnecessarily time-consuming, but it is up to you.

I am a little tired now, so I'm sorry for all the things that seem funny to me, but I hope I helped, and please tell me if I completely missed your point of view and I can try to find a solution.

0
source

All Articles