且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UINavigationBar 多行标题

更新时间:2023-12-03 14:38:10

设置 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.