UINavigationBar multiline name

Is there an easy way to override the titleView of the current navigation bar item in the navigation bar in the navigation controller? I tried to create a new UIView and replace the titleView topView property with my own UIVIew without success.

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

+45
uilabel uinavigationitem title uinavigationcontroller uinavigationbar
Mar 11
source share
6 answers

Set the titleView property to UINavigationItem . For example, in the view controller method viewDidLoad you can 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 

It is displayed as follows:

multi-line titlebar label

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

+78
Apr 12 2018-10-12T00: 00Z
source share
— -

Swift Solution:

2 lines in the NavigationBar :

 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 row in 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 

the result is

enter image description here

+16
Oct 10 '16 at 11:32
source share

for 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 
+15
Sep 19 '15 at 23:37
source share

After setting up multiple times, I still couldn’t get the petert solution for me in iOS 8. Here is the paste solution for iOS 8/9. The loan goes to Matt Curtis github post

 - (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; }); } } 
+5
Nov 30 '15 at 20:53
source share

Here is the version of Swift 3 handling multi-line header:

 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 } 
+2
Jun 21 '17 at 17:57
source share

What to do if the mark is not centered

If you encounter the same problem as me, this label is not concentrated in the navigationItem function due to the back button, insert your UILabel in the UIView. UILabel is then not forced to grow with text, but stops growing when width increases the width of the view. You can find more about this problem here: It is not possible to set the titleView in the center of the navigation bar because the back button (Darren's answer)

Not centered:

enter image description here

 - (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; } 

Concentrated:

enter image description here

 - (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; } 
+1
Apr 6 '17 at 16:35
source share



All Articles