不同的UINavigationControllers中出现不同的外观

时间:2017-10-11 15:19:49

标签: ios swift uinavigationcontroller uinavigationbar uiappearance

在iOS 11中,我正在改变导航控制器的后退按钮,如下所示:

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

这似乎工作正常,问题是当我想区分使用不同背面图像的两种UINavigationControllers时:

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)

使用第二种方法,会显示常规后退按钮,因此无论如何它都无法识别更改。有谁知道我的方法有什么问题?

1 个答案:

答案 0 :(得分:3)

我已在侧面项目中重新创建了您的场景,并且您呈现的代码段工作正常。我认为视图控制器层次结构中可能存在某些内容。

这就是我构建层次结构的方式。 ViewController有一个按钮,可以在导航堆栈上推送另一个UIViewController

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

这就是我设置外观的方式。

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

这是结果

enter image description here enter image description here