Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we add additional UILabels to UINavigation Bar besides its title?

I am working on one app in which I have to display app's status on the UINavigation Bar. I am going to use UILabel to display the status. I was wondering whether it is possible or not ?

Regards, Sumit

like image 306
slonkar Avatar asked Dec 17 '22 05:12

slonkar


2 Answers

Yes. The things to do are

  • create your view you want to display (subclass of UIView) which contains all your labels.
    • if you just want to show one single label than this first step is not needed because you can use the UILabel directly
  • wrap your view in an UIBarButtonItem with the help of initWithCustomView:myLabelsView
  • call setRightBarButtonItem:animated: with your UIBarButtonItem-instance on [myViewController.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO]

Example:

-(void) viewDidLoad {
    ....
    UIView *myNiceLabelView = [UILabel ...];
    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myNiceLabelView];
    [self.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO];
    ....
}
like image 80
thomas Avatar answered Jan 30 '23 13:01

thomas


I don't like to see crazy user interfaces (and I don't think your users would like it either -- there isn't all that much real estate or space to work with in the navigation bar), but the technically correct answer for you here is yes you can:

Create a view with multiple labels (or whatever you want) embedded in it and set your UINavigationItem's titleView property to that.

like image 22
Michael Dautermann Avatar answered Jan 30 '23 14:01

Michael Dautermann