在 iOS 11 中使用增加的导航栏标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44409173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use the increased navigation-bar title in iOS 11
提问by Hans Kn?chel
iOS 11 Beta 1 uses the increased navigation-bar title for almost all system-apps (it started doing this in iOS 10 and the Music app). I am wondering if Apple has a public API for this coming in iOS 11, or whether it will stay private for now.
iOS 11 Beta 1 为几乎所有系统应用程序使用增加的导航栏标题(它在 iOS 10 和音乐应用程序中开始这样做)。我想知道 Apple 是否在 iOS 11 中有一个公共 API,或者它现在是否会保持私有。
The behavior is that the title has an increased font-size, is left aligned and will move to the navigation-bar once the user scrolls down. I've attached some screens showing this behavior in the Messages app here:
行为是标题具有增加的字体大小,左对齐并且一旦用户向下滚动就会移动到导航栏。我在此处的消息应用程序中附加了一些显示此行为的屏幕:
Although I could not find any reference in the UINavigationController
and UINavigationBar
so far, maybe someone knows some more details!
虽然我无法找到任何的参考UINavigationController
和UINavigationBar
到目前为止,也许有人知道一些细节!
回答by Moin Shirazi
The only change done to UINavigationBar
API for iOS 11 is prefersLargeTitles
.
UINavigationBar
对 iOS 11 的 API所做的唯一更改是prefersLargeTitles
.
Documentation here: https://developer.apple.com/documentation/uikit/uinavigationbar/
此处的文档:https: //developer.apple.com/documentation/uikit/uinavigationbar/
You can do it to your own apps with one small change: check "Prefers Large Titles" for your navigation bar in IB, or if you prefer to do it in code using:
您可以通过一个小的更改对您自己的应用程序执行此操作:在 IB 中为您的导航栏选中“首选大标题”,或者如果您更喜欢使用以下代码在代码中执行此操作:
navigationController?.navigationBar.prefersLargeTitles = true
If you need to change the text attributes of the large title you need to use the new largeTitleTextAttributes
property on UINavigationBar
:
如果您需要更改大标题的文本属性,则需要在 上使用新largeTitleTextAttributes
属性UINavigationBar
:
UINavigationBar.appearance().largeTitleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.black
]
回答by kgaidis
UINavigationBar
has a prefersLargeTitles: Bool
property. Docs here.
UINavigationBar
有一个prefersLargeTitles: Bool
属性。文档在这里。
class UINavigationBar {
var prefersLargeTitles: Bool
}
UINavigationItem
has a largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode
property. Docs here.
UINavigationItem
有一个largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode
属性。文档在这里。
class UINavigationItem {
var largeTitleDisplayMode: LargeTitleDisplayMode
}
Both of these can be modified in the Interface Builder.
这两个都可以在 Interface Builder 中修改。
To turn on this behavior set navigationController.navigationBar.prefersLargeTitles
to true
. Then you can control each individual view controller in the navigation controller stack by setting navigationItem.largeTitleDisplayMode
.
要打开此行为设置navigationController.navigationBar.prefersLargeTitles
为true
。然后你可以通过设置来控制导航控制器堆栈中的每个单独的视图控制器navigationItem.largeTitleDisplayMode
。
The general design guidelines by Apple are that large titles shouldn't be used everywhere (for example, the Clock app does not use them), and it's generally preferred that only the first level of the navigation controller uses the large titles. However, these are just general guidelines.
Apple 的一般设计准则是不应在任何地方使用大标题(例如,时钟应用程序不使用它们),并且通常首选导航控制器的第一级使用大标题。但是,这些只是一般准则。
Large titles are introduced in What's New in Cocoa Touch video(7:37).
大标题在 Cocoa Touch 的新增功能视频(7:37) 中介绍。
回答by Maor
回答by Jordi Bruin
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
} else {
// Fallback on earlier versions
}
Note that there are some bugs in beta 1 which cause the large title to only appear when you manually scroll up.
请注意,beta 1 中有一些错误会导致大标题仅在您手动向上滚动时出现。
回答by Sai Sandeep
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.topItem?.title = "Hello"
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
let attributes = [
NSAttributedStringKey.foregroundColor : UIColor.red,
]
navigationController?.navigationBar.largeTitleTextAttributes = attributes
} else {
// Fallback on earlier versions
}
回答by Lavrin Pristazh
Since large titles are available since iOS 11 you also have to check for iOS version:
由于大标题自 iOS 11 起可用,您还必须检查 iOS 版本:
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
}