UINavigationBar 多行标题 [英] UINavigationBar multi-line title

查看:37
本文介绍了UINavigationBar 多行标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种直接的方法可以覆盖导航控制器中导航栏中当前导航栏项的 titleView?我尝试创建一个新的 UIView 并用我自己的 UIVIew 替换 topView 的 titleView 属性,但没有成功.

Is there a straightforward way of overriding the titleView of the current navigation bar item in a navigation bar within a navigation controller? I've tried creating a new UIView and replacing the titleView property of topView with my own UIVIew with no success.

基本上,我想要导航栏标题的多行标题.有什么建议吗?

Basically, I want a multi-line title for the navigation bar title. Any suggestions?

推荐答案

设置 UINavigationItemtitleView 属性.例如,在视图控制器的 viewDidLoad 方法中,您可以执行以下操作:

Set the titleView property of the UINavigationItem. For example, in the view controller's viewDidLoad method you could do something like:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = @"This is a\nmultiline string";

self.navigationItem.titleView = label;

#if !__has_feature(objc_arc)
[label release];
#endif

显示如下:

记住 titleView 属性被忽略 如果 leftBarButtonItem 不是 nil.

Remember the titleView property is ignored if leftBarButtonItem is not nil.

这篇关于UINavigationBar 多行标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆