tonik12
tonik12

Reputation: 613

Different appearances in different UINavigationControllers

In iOS 11 I'm currently changing the back button for my navigation controllers like this:

UINavigationBar.appearance().backIndicatorImage = whiteBackButtonImage!.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorTransitionMaskImage = whiteBackButtonImage!.withRenderingMode(.alwaysOriginal)

That seems to work fine, the problem is when I want to differentiate between two kind of UINavigationControllers using different back images :

let whiteNavigationBarAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [WhiteNavigationController.self])
whiteNavigationBarAppearance.backIndicatorImage = greenBackButtonImage!.withRenderingMode(.alwaysOriginal)
whiteNavigationBarAppearance.backIndicatorTransitionMaskImage = greenBackButtonImage!.withRenderingMode(.alwaysOriginal)

let greenNavigationBarAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [GreenNavigationController.self])
greenNavigationBarAppearance.backIndicatorImage = whiteBackButtonImage!.withRenderingMode(.alwaysOriginal)
greenNavigationBarAppearance.backIndicatorTransitionMaskImage = whiteBackButtonImage!.withRenderingMode(.alwaysOriginal)

With the second approach, the regular back button is shown, so somehow it doesn't recognise the changes. Does anyone know what's wrong in my approach?

Upvotes: 9

Views: 614

Answers (1)

Kamil Szostakowski
Kamil Szostakowski

Reputation: 2163

I have recreated your scenario on the side project and the snippet you presented works just fine. I think there may be something in your view controllers hierarchy.

This is how I build the hierarchy. ViewController has a button which pushes another UIViewController on the navigation stack.

let redViewController = ViewController()
redViewController.view.backgroundColor = .red

let greenViewController = ViewController()
greenViewController.view.backgroundColor = .green

let red = RedNavigationController(rootViewController: redViewController)
red.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)

let green = GreenNavigationController(rootViewController: greenViewController)
green.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 2)

let tabBarController = UITabBarController()
tabBarController.setViewControllers([red, green], animated: false)
tabBarController.selectedIndex = 0

This is how I set up the appearance.

let image1 = UIImage(named: "Button")!.withRenderingMode(.alwaysOriginal)
let image2 = UIImage(named: "Button2")!.withRenderingMode(.alwaysOriginal)

let red = UINavigationBar.appearance(whenContainedInInstancesOf: [RedNavigationController.self])
red.backIndicatorImage = image1
red.backIndicatorTransitionMaskImage = image1

let green = UINavigationBar.appearance(whenContainedInInstancesOf: [GreenNavigationController.self])
green.backIndicatorImage = image2
green.backIndicatorTransitionMaskImage = image2

And this is the result

enter image description here enter image description here

Upvotes: 3

Related Questions