1

I added a UINavigationBar in storyboard. My app has to change the title of the bar dynamically. First I tried changing the title in storyboard and ran the app, the title didn't appear. I then tried adding the title by code. First, I connected an IBOutlet from the UINavigationItem to my @interface and tried adding a title like this:

_navibarTitle.title = @"Something";

It also didn't work out, the title doesn't appear. Since my app has to hide the default bar of the UINavigationController and use custom ones, I even tried this:

self.navigationItem.title = @"Something";

and this:

self.navigationController.navigationItem.title = @"HI";

they also didn't work out. (the last two i know they wouldn't work as the default navi bar is hidden). So i guess i'm out of ideas. What could be wrong?

iBug
  • 2,334
  • 3
  • 32
  • 65
HusseinB
  • 1,321
  • 1
  • 17
  • 41
  • Do you try to NSLog the self.navigationController to make sure it is exist? Or Just try to set the title of the view controller -- self.title = @"your title" which will be displayed as the navigation title if you does not set explicitly. – Danyun Liu Jul 07 '14 at 08:17
  • @Danyun yes sure i tried NSLoging where the block of code exists and it prints out. So yes it's executing the code – HusseinB Jul 07 '14 at 08:19
  • 1
    Set title name of UIViewController which pushed to navigation controller. It will change the title of navigation. – Ryan Jul 07 '14 at 08:21
  • Danyun isn't asking you to check if it's executing that code block you have above, Danyun is asking if you have actually embedded your UIViewController inside a UINavigationController. You don't manually add UINavigationBar to the interface, that comes with a UINavigationController automatically when you set a root view controller for an instance of UINavigationController. I think he means tried adding this line of code NSLog(@"self.navigationController = %@", self.navigationController); – Zhang Jul 07 '14 at 08:37
  • you were added the navigation controller for navigation – Anbu.Karthik Jul 07 '14 at 08:49

2 Answers2

1

The simple and best way to set title for a screen or navigation bar is:

self.title = @"Your Title"

It will show your title on navigation bar automatically. You can get help from this question as well.

Other way to do same:

    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"Your Title";
    self.navigationItem.titleView = navLabel;

I hope this will work.

Community
  • 1
  • 1
iBug
  • 2,334
  • 3
  • 32
  • 65
  • yes, this will work, but if you want something custom, like custom color, or custom font, you can't achieve it with this aproach – Bogdan Somlea Jul 07 '14 at 08:26
0

try this:

self.navigationController.navigationBar.tintColor = [UIColor redColor];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:20];
self.navigationItem.titleView = titleLabel;
titleLabel.text = @"Something";
[titleLabel sizeToFit];
Bogdan Somlea
  • 604
  • 3
  • 15