iOS11 如何在导航栏上设置大标题?
How to set large title on navigation bar for iOS 11?
我正在为 iOS 11 或更高版本创建应用程序,要求在导航栏左侧设置大标题。
请大神帮忙看看怎么设置,应该只对iOS 11或更高版本有效。
给我一些其他建议,以在整个应用程序(支持 iOS 8 或更高版本)中保持此功能可用。
提前致谢。
这里是在 iOS 11 或更高版本的导航栏左侧显示大标题的代码片段。
Objective C:
self.title = @"Your title";
if (@available(iOS 11, *)) {
self.navigationController.navigationBar.prefersLargeTitles = true;
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
}
Swift:
self.title = "Your title"
if #available(iOS 11, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
}
在构建应用程序之前,您需要为 iOS 11 添加检查条件。
测试大标题的要求:
- Xcode 9.0,
- Mac OSX - 10.12.6 或更高版本,
- iPhone/iPad 或 Xcode 9 模拟器与 iOS 11.
if (@available(iOS 11.0, *)) {
[[UINavigationBar appearance] setPrefersLargeTitles:false];
}
您可以将UILabel设置为titleView。
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = YOUR_TITLE_TEXT;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.font = FONT_NAV_BAR;
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;
我正在为 iOS 11 或更高版本创建应用程序,要求在导航栏左侧设置大标题。
请大神帮忙看看怎么设置,应该只对iOS 11或更高版本有效。
给我一些其他建议,以在整个应用程序(支持 iOS 8 或更高版本)中保持此功能可用。
提前致谢。
这里是在 iOS 11 或更高版本的导航栏左侧显示大标题的代码片段。
Objective C:
self.title = @"Your title";
if (@available(iOS 11, *)) {
self.navigationController.navigationBar.prefersLargeTitles = true;
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
}
Swift:
self.title = "Your title"
if #available(iOS 11, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .always
}
在构建应用程序之前,您需要为 iOS 11 添加检查条件。
测试大标题的要求:
- Xcode 9.0,
- Mac OSX - 10.12.6 或更高版本,
- iPhone/iPad 或 Xcode 9 模拟器与 iOS 11.
if (@available(iOS 11.0, *)) {
[[UINavigationBar appearance] setPrefersLargeTitles:false];
}
您可以将UILabel设置为titleView。
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = YOUR_TITLE_TEXT;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.font = FONT_NAV_BAR;
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;