在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:12:38  来源:igfitidea点击:

Use the increased navigation-bar title in iOS 11

iosiphoneuinavigationbarxcode9-betaios11

提问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:

行为是标题具有增加的字体大小,左对齐并且一旦用户向下滚动就会移动到导航栏。我在此处的消息应用程序中附加了一些显示此行为的屏幕:

enter image description here

在此处输入图片说明

Although I could not find any reference in the UINavigationControllerand UINavigationBarso far, maybe someone knows some more details!

虽然我无法找到任何的参考UINavigationControllerUINavigationBar到目前为止,也许有人知道一些细节!

回答by Moin Shirazi

The only change done to UINavigationBarAPI 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 largeTitleTextAttributesproperty on UINavigationBar:

如果您需要更改大标题的文本属性,则需要在 上使用新largeTitleTextAttributes属性UINavigationBar

UINavigationBar.appearance().largeTitleTextAttributes = [
    NSAttributedString.Key.foregroundColor: UIColor.black
]

回答by kgaidis

UINavigationBarhas a prefersLargeTitles: Boolproperty. Docs here.

UINavigationBar有一个prefersLargeTitles: Bool属性。文档在这里

class UINavigationBar {
   var prefersLargeTitles: Bool
}

UINavigationItemhas a largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayModeproperty. 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.prefersLargeTitlesto true. Then you can control each individual view controller in the navigation controller stack by setting navigationItem.largeTitleDisplayMode.

要打开此行为设置navigationController.navigationBar.prefersLargeTitlestrue。然后你可以通过设置来控制导航控制器堆栈中的每个单独的视图控制器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

Just check "Prefers Large Titles" in the Navigation Bar attribute inspector in Storyboard / Interface Builder:

只需在 Storyboard / Interface Builder 的导航栏属性检查器中选中“Prefers Large Titles”:

enter image description here

在此处输入图片说明

回答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
}