首页 > 解决方案 > 我可以从自定义视图类更改 NavigationController 设置吗

问题描述

我可以从自定义视图类更改 NavigationController 设置吗?

import UIKit

class TutorialView: UIView {
  init() {
    //change navigation controller settings
    //ex: navigationController.navigationItem.title = "hoge"
  }
}

我知道这种方式。但是我的应用程序 rootViewController 是 TabBarController。TabBarController 上的 NavigationController。

(window?.rootViewController as? UINavigationController)?.navigationItem.title = "hoge"

以什么方式最好更改 NavigationController 设置?

标签: swiftswift3

解决方案


ViewControllerss viewDidLoadMethod 是更改 NavigationController 任何属性的正确位置,IMO,只要它们特定于该视图。这些包括标题颜色、大标题显示设置等...如果您想更改/添加 NavigationController 方法,例如实现新的 pushViewController 方法,那么我建议您子类UINavigationController化并覆盖必要的属性/方法。如果你想改变导航栏的外观,你可以从你的 AppDelegate 中做到这一点,见这个


推荐阅读