iOS14 navigationItem.largeTitleDisplayMode = .always not work(iOS14 navigationItem.largeTitleDisplayMode = .always 不工作)
问题描述
我有一个ViewController
和一个DetailViewController
,在ViewController
的ViewDidLoad
我设置如下代码,目的是让ViewController
总是使用大标题
I have a ViewController
and a DetailViewController
, in the ViewDidLoad
of the ViewController
I set the following code, the purpose is to make the ViewController
always use the large title
self.navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
在DetailViewController
的ViewDidLoad
中我设置如下代码,目的是让DetailViewController
不使用大标题
In the ViewDidLoad
of the DetailViewController
I set the following code, the purpose is to make the DetailViewController
not use the large title
navigationItem.largeTitleDisplayMode = .never
当我从 DetailViewController
返回到 ViewController
时,ViewController
中显示的是小标题而不是大标题.此代码在 iOS12 和 iOS13 中是正确的.如何让ViewController
在iOS14上一直显示大标题?
When I return from DetailViewController
to ViewController
, the small title is displayed instead of the large title in ViewController
. This code is correct in iOS12 and iOS13. How to make the ViewController
always display the large title on iOS14?
目前正在使用 App Store 中的 Xcode12
Currently using Xcode12 from the App Store
推荐答案
使用我的扩展:
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}
}
在 viewWillAppear 中设置导航栏:
set your navigation bar in viewWillAppear:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureNavigationBar(largeTitleColor: .white, backgoundColor: .yourColor, tintColor: .white, title: "YourTitle", preferredLargeTitle: true)
}
现在调用显示你的细节控制器的函数:
now cal the function that present your detail controller:
@objc func DetailController(){
let controller = DetailViewController()
controller.navigationItem.largeTitleDisplayMode = .never
navigationController?.pushViewController(controller, animated: true)
}
这是结果:
这篇关于iOS14 navigationItem.largeTitleDisplayMode = .always 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS14 navigationItem.largeTitleDisplayMode = .always 不工作


基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01