NoSixties NoSixties - 1 month ago 13
Swift Question

Create a custom UINavigationcontroller class

I have done the following customisation to my embedded UINavigationcontroller. I've done this in the view controller of the view that will show up as first.

However I'm trying to clean it up so that I don't have this all in my UIView class by creating a separate class that will take care of this. The code I'm currently using

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "navigation.background"), for: .default)

let backButton = UIImage(named: "Back.button")?.withRenderingMode(.alwaysOriginal)
let backButtonHigh = UIImage(named: "Back.button.highlighted")?.withRenderingMode(.alwaysOriginal)


self.navigationController?.navigationBar.backIndicatorImage = backButton
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonHigh
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)


What I've tried to do is extend the UInavigationcontroller in a new class like this

class RSRNavigationController: UINavigationController{
override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "navigation.background"), for: .default)

let backButton = UIImage(named: "Back.button")?.withRenderingMode(.alwaysOriginal)
let backButtonHigh = UIImage(named: "Back.button.highlighted")?.withRenderingMode(.alwaysOriginal)

self.navigationController?.navigationBar.backIndicatorImage = backButton
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonHigh
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}


this doesn't work. It will run without problems but it won't actually do anything.

I've also tried to do the following

class RSRNavigationController: UINavigationBar{
override func draw(_ rect: CGRect) {
super.draw(rect)
self.setBackgroundImage(UIImage(named: "navigation.background"), for: .default)
let backButton = UIImage(named: "Back.button")?.withRenderingMode(.alwaysOriginal)
let backButtonHigh = UIImage(named: "Back.button.highlighted")?.withRenderingMode(.alwaysOriginal)
self.backIndicatorImage = backButton
self.backIndicatorTransitionMaskImage = backButtonHigh
//self.backItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}


However I can't seem to set the title for the back button.

Does anyone know how to fix this?

Answer

Your problem lies here, when you reference navigationController INSIDE your custom navigationController class

Instead of doing

self.navigationController?.navigationBar.backIndicatorImage = backButton

do this

self.navigationBar.backIndicatorImage = backButton

Reason?

Because when you extend your custom class by UINavigationController, it becomes a navigationController itself. And by doing self.navigationController you are telling your app to look for a navigationController within which your custom navigationCOntroller resides. Ofcourse such a nav controller does not exist because your custom navController is the main or the top navController of the app. So just remove the self.navigationController part from your custom class, the rest of the code that you have written in your viewDidLoad should work good.

If you still have any questions feel free to ask