UINavigationBar多行标题

有没有一种直接的方式来覆盖导航控制器中的导航栏中当前导航栏项目的titleView? 我已经尝试创build一个新的UIView,并用我自己的UIVIewreplacetopView的titleView属性没有成功。

基本上,我想要一个导航栏标题的多行标题。 有什么build议么?

设置UINavigationItemtitleView属性。 例如,在视图控制器的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 

它显示如下:

多行标题栏标签

记住,如果leftBarButtonItem不是niltitleView属性将被忽略 。

Swift解决scheme:

NavigationBar 2行:

 private func setupTitleView() { let topText = NSLocalizedString("key", comment: "") let bottomText = NSLocalizedString("key", comment: "") let titleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(), NSFontAttributeName : UIFont.<Font>] let subtitleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(), NSFontAttributeName : UIFont.<Font>] let title:NSMutableAttributedString = NSMutableAttributedString(string: topText, attributes: titleParameters) let subtitle:NSAttributedString = NSAttributedString(string: bottomText, attributes: subtitleParameters) title.appendAttributedString(NSAttributedString(string: "\n")) title.appendAttributedString(subtitle) let size = title.size() let width = size.width guard let height = navigationController?.navigationBar.frame.size.height else {return} let titleLabel = UILabel(frame: CGRectMake(0,0, width, height)) titleLabel.attributedText = title titleLabel.numberOfLines = 0 titleLabel.textAlignment = .Center navigationItem.titleView = titleLabel } 

2 BarButton线

  let string = NSLocalizedString("key", comment: "") let attributes = [NSForegroundColorAttributeName : UIColor.<Color>, NSFontAttributeName : UIFont.<Font>] let size = (string as NSString).sizeWithAttributes(attributes) guard let height = navigationController?.navigationBar.frame.size.height else {return} let button:UIButton = UIButton(frame: CGRectMake(0, 0, size.width, height)) button.setAttributedTitle(NSAttributedString(string: string, attributes: attributes), forState: .Normal) button.addTarget(self, action: #selector(<SELECTOR>), forControlEvents: .TouchUpInside) button.titleLabel?.numberOfLines = 0 button.titleLabel?.textAlignment = .Right let rightBarButton = UIBarButtonItem(customView: button) navigationItem.rightBarButtonItem = rightBarButton 

结果 –

在这里输入图像说明

对于Swift:

 let label = UILabel(frame: CGRectMake(0, 0, UIScreen.main.bounds.width, 44)) label.backgroundColor = UIColor.clearColor() label.numberOfLines = 0 label.textAlignment = NSTextAlignment.Center label.text = "multiline string" self.navigationItem.titleView = label 

经过很多的调整,我仍然无法让Petert的解决scheme在iOS 8中为我工作。下面是适用于iOS 8/9的可复制粘贴解决scheme。 值得一提的是Matt Curtis的github博文

 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if(!self.navigationItem.titleView){ self.navigationItem.titleView = ({ UILabel *titleView = [UILabel new]; titleView.numberOfLines = 0; titleView.textAlignment = NSTextAlignmentCenter; titleView.attributedText = [[NSAttributedString alloc] initWithString:@"2\nLINES" attributes: self.navigationController.navigationBar.titleTextAttributes ]; [titleView sizeToFit]; // You'll need to set your frame otherwise if your line breaks aren't explcit. titleView; }); } } 

这是处理多行标题的Swift 3版本:

 override func viewDidLoad() { super.viewDidLoad() let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)) label.backgroundColor = .clear label.numberOfLines = 0 label.textAlignment = .center label.font = UIFont.boldSystemFont(ofSize: 14.0) label.text = "This is a Multi-Line title of UINavigationBar" self.navigationItem.titleView = label } 

标签不居中时该怎么办

如果遇到与我相同的问题 – 由于后退button导致标签不在导航项中,请将UILabelembedded到UIView中。 然后,UILabel不会被强制与文本一起增长,而是在宽度提高视图的宽度时停止增长。 更多关于这个问题,你可以在这里find: 不能在导航栏的中心设置titleView,因为后退button (达伦的答案)

不居中:

在这里输入图像说明

 - (void)setTwoLineTitle:(NSString *)titleText color:(UIColor *)color font:(UIFont *)font { CGFloat titleLabelWidth = [UIScreen mainScreen].bounds.size.width/2; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleLabelWidth, 44)]; label.backgroundColor = [UIColor clearColor]; label.numberOfLines = 2; label.font = font; label.adjustsFontSizeToFitWidth = YES; label.textAlignment = UITextAlignmentCenter; label.textColor = color; label.text = titleText; self.navigationItem.titleView = label; } 

居中:

在这里输入图像说明

 - (void)setTwoLineTitle:(NSString *)titleText color:(UIColor *)color font:(UIFont *)font { CGFloat titleLabelWidth = [UIScreen mainScreen].bounds.size.width/2; UIView *wrapperView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, titleLabelWidth, 44)]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleLabelWidth, 44)]; label.backgroundColor = [UIColor clearColor]; label.numberOfLines = 2; label.font = font; label.adjustsFontSizeToFitWidth = YES; label.textAlignment = UITextAlignmentCenter; label.textColor = color; label.text = titleText; [wrapperView addSubview:label]; self.navigationItem.titleView = wrapperView; }