SwiftUI navigationBarTitle
修饰符具有可选的displayMode
属性,对于小标题,可以将其设置为.inline
,对于大标题,可以将其设置为.large
。 See documentation
NavigationView {
TopLevelView {
// […]
}
.navigationBarTitle("Test", displayMode: .inline) // ⬅️ Important part
}
自iOS 11起,UINavigationBar
可以以标准和大标题模式显示其标题。
在UIKit]上,如果要在两种行为之间进行选择,则必须设置ViewController的largeTitleDisplayMode
的navigationItem
属性,以决定此特定的视图控制器是否应显示大标题。
然后,您需要检查导航控制器prefersLargeTitle
的navigationBar
属性。将其设置为true
将允许其导航堆栈中的ViewController显示大标题。相反,将其设置为false
将阻止它,从而覆盖堆栈中存在的各个NavigationItem的首选项。
这将在UIKit中显示一个大标题
// Set this property to true to allow NavigationItems to display large titles
let navigationController = UINavigationController()
navigationController.navigationBar.prefersLargeTitles = true
/*
Choose between `always`, `never` and `automatic` to decide
if this particular view controller should display a large title.
*/
let viewController = UIViewController()
viewController.navigationItem.largeTitleDisplayMode = .always