是否有一种直接的方法可以覆盖导航 Controller 内导航栏中当前导航栏项目的 titleView?我尝试创建一个新的 UIView 并用我自己的 UIVIew 替换 topView 的 titleView 属性,但没有成功。
基本上,我想要一个多行标题作为导航栏标题。有什么建议吗?
最佳答案
设置UINavigationItem
的titleView
属性。例如,在 View Controller 的 viewDidLoad
方法中,您可以执行以下操作:
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
属性是ignored如果 leftBarButtonItem
不是 nil
。
关于uinavigationcontroller - UINavigationBar多行标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2422383/