如何将 SwiftUI 中的 NavigationView 的标题设置为大标题(或小标题)? [英] How to set the title of a NavigationView in SwiftUI to large title (or small)?
问题描述
使用 SwiftUI 如何更改导航栏的标题大小?在标准或大标题之间进行选择.
Using SwiftUI how do I change the navigation bar's title size? Choosing between a standard or a large title.
推荐答案
SwiftUI (Xcode 11.3)
SwiftUI navigationBarTitle
修饰符有一个可选的 displayMode
属性,您可以将其设置为 .inline
用于小标题和 .large代码> 用于大标题.查看文档
SwiftUI (Xcode 11.3)
SwiftUI navigationBarTitle
modifier has an optional displayMode
property which you can set to .inline
for small titles and .large
for large titles. See documentation
NavigationView {
TopLevelView {
// […]
}
.navigationBarTitle("Test", displayMode: .inline) // ⬅️ Important part
}
<小时>
它是如何在 UIKit 中完成的
从 iOS 11 开始,UINavigationBar
可以在标准和大标题模式下显示其标题.
How it's done in UIKit
Since iOS 11, UINavigationBar
can display its title in standard and large title mode.
在 UIKit 上,如果您想在两种行为之间进行选择,您必须设置 ViewController 的 navigationItem
的 largeTitleDisplayMode
属性来决定是否这个特定的视图控制器应该显示一个大标题或不显示.
On UIKit, if you want to choose between the two behaviors you have to set the largeTitleDisplayMode
property of your ViewController's navigationItem
to decide if this particular view controller should display a large title or not.
然后,您需要检查导航控制器的navigationBar
的prefersLargeTitle
属性.将其设置为 true
将允许其导航堆栈中的 ViewController 显示大标题.相反,将其设置为 false
将阻止它,覆盖堆栈中存在的各个 NavigationItems 的首选项.
Then, you need to check the prefersLargeTitle
property of your Navigation Controller's navigationBar
. Setting it to true
will allow the ViewControllers in its navigation stack to display large titles. Conversely, setting it to false
will prevent it, overriding the preference of the individual NavigationItems present in the stack.
这将在 UIKit 中显示一个大标题
This will display a large title in 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
这篇关于如何将 SwiftUI 中的 NavigationView 的标题设置为大标题(或小标题)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!