问题描述
我可以从自定义视图类更改 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 设置?
解决方法
ViewControllers
s viewDidLoad
方法是更改 NavigationController 的任何属性的正确位置,IMO,只要它们特定于该视图。这些包括标题颜色,大标题显示设置等...
如果您想更改/添加 NavigationController 方法,例如实现新的 pushViewController 方法,那么我建议您继承 UINavigationController
并覆盖必要的属性/方法。
如果您想更改导航栏的外观,您可以从您的 AppDelegate 执行此操作,请参阅 this。