我的应用程序中有一个问题,在View1上设置了一个很大的标题,但是当我启动View2时,标题突然又变小了。因此,当按下View2上的后退按钮时,View1将启动,其中LargeTitle将从小到大对齐。
我用来创建LargeTitle的代码是“NavigationItem.LargeTitleDisplayMode=.Always”,它位于ViewWillIncere中。
视图1-
‘重写函数viewwillopere(_animated:Bool){super.viewwillopere(animated)
navigationItem.largeTitleDisplayMode = .always
if #available(iOS 12.0, *) {
self.navigationController?.navigationBar.barTintColor = UIColor(named: "#222222")
self.navigationController?.navigationBar.isTranslucent = true;
self.navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
“
视图2-
‘重写函数viewwillopere(_animated:Bool){super.viewwillopere(animated)
self.navigationController?.setToolbarHidden(true, animated: false)
navigationController?.navigationBar.prefersLargeTitles = false
if #available(iOS 13.0, *) {
UIApplication.shared.statusBarStyle = .darkContent
}
if #available(iOS 12.0, *) {
self.navigationController?.navigationBar.barTintColor = UIColor.white
self.navigationController?.navigationBar.isTranslucent = false;
self.navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.black]
}
self.navigationItem.titleView = setTitle(title: name, titleColor: UIColor.black, titleSize: 14, subtitle: category, subtitleColor: UIColor.gray, subtitleSize: 12, view: self.view)'
‘重写函数viewwilldiscount(_animated:Bool){super.viewwilldiscount(animated)
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .always
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
self.navigationController?.navigationBar.isTranslucent = true;
}'
这是我使用的与两个视图的标题文本相关联的代码。故事板嵌套在一个NavigationController中,因此这可能与此有关。
注意-我没有通过故事板设置LargeTitle,但是这样做并不能解决问题。
下面是问题的视频片段。:
对齐LargeTitle:-
我认为问题在于,当NavigationItem.LargetItleDisplayMode=.Alway'即将消失时,您需要在第二个视图中恢复它
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationItem.largeTitleDisplayMode = .always'
}