HusseinB
HusseinB

Reputation: 1341

Custom navigation bar doesn't change / show title

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?

Upvotes: 1

Views: 804

Answers (2)

iBug
iBug

Reputation: 2352

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.

Upvotes: 1

Bogdan Somlea
Bogdan Somlea

Reputation: 624

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];

Upvotes: 0

Related Questions