ios - 标题有什么不同?

标签 ios swift uinavigationcontroller uinavigationbar uinavigationitem

我想为我的 UIViewController 添加标题:

我试过了

self.navigationController.navigationItem.title
self.navigationController.title
self.title

有时,解决方案 1 有效,有时解决方案 2 有效,有时解决方案 3 有效。

哪位高手能告诉我它们之间的区别吗?

最佳答案

titleUIViewController 的属性。

A localized string that represents the view this controller manages. Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text in those objects.


self.navigationController 是一个 UINavigationController,它管理 viewController 所在的 viewController 堆栈。UINavigationControllerUIViewController 的子类,所以 self.navigationController.titleUINavigationControllertitle


self.navigationItem.title:

The navigation item’s title displayed in the center of the navigation bar. The default value is nil. When the receiver is on the navigation item stack and is second from the top—in other words, its view controller manages the views that the user would navigate back to—the value in this property is used for the back button on the top-most navigation bar. If the value of this property is nil, the system uses the string “Back” as the text of the back button.


因此,在实践中,您应该设置 ViewControllertitle。如果您的 ViewControllerUINavigationController 管理,iOS 会将此标题复制到导航项或标签栏项并在导航栏上显示此标题,当您按下另一个 ViewController 时,它会将该文本用于 Back Button,并且如果您的 ViewController 将它显示在选项卡栏中由 UITabBarController 管理。

关于ios - 标题有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41208404/

相关文章:

ios - UIBarButton 不执行操作

Swift - 添加一个覆盖现有导航栏的导航栏

iphone - 改变 ViewController 的转换方向?

iphone - Coredata错误setObjectForKey : object cannot be nil

iphone - 等待准确的 didUpdateToLocation

ios - 动画 UIImageView 从圆角右上角开始并以矩形结束

ios - 使用 tabBarControllers 快速导航到特定子 VC

ios - 用数组 react native 状态

ios - iOS 14.3 上的 UICollectionViewCompositionalLayout 错误

ios - 完成转换时的 io7 导航检查