Eli Waxman
Eli Waxman

Reputation: 2897

Want to change NavigationItem title

I've embedded my first view into a NavigationController and then i set the segue as push. I want to change in each view the title of the Navigation bar to display something and update it through my code. i tried to call the navigation controller like this:

self.navigationController.navigationBar.topItem.title = @"YourTitle";

but this wont work, how can I do it?

Upvotes: 5

Views: 11574

Answers (5)

Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40247

self.title = @"My Title"

This will change the navigation bars title as well as the title elements that refer to this view controller (eg. tab bar etc.)

self.navigationItem.title = @"My Title";

This will change only the title for the navigation bar for current view controller.

Upvotes: 2

Shashi Sharma
Shashi Sharma

Reputation: 126

if u only change the navigation bar title then try this on initwithnibname method

        self.title=@"your title";

but you want to give a color or font size of title then u want to take a UIlable on it

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];

        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;

        label.textColor = [UIColor blackColor];
        self.navigationItem.titleView = label;
        label.text =@"your title";
        [label sizeToFit];

Upvotes: 0

stosha
stosha

Reputation: 2148

You can replace title view too:

UIView* titleView = [[UIView alloc] init];
[self.navigationItem setTitleView: titleView];

Upvotes: 0

John
John

Reputation: 2694

This works for me:

self.navigationItem.title = @"YourTitle"; 

Hope it helps.

Upvotes: 5

Lithu T.V
Lithu T.V

Reputation: 20021

Navigation controller gets the title from the viewcontroller so this will do the job

- (void)viewDidLoad {

   [super viewDidLoad];
   self.title = @"Some Title";
}

Upvotes: 14

Related Questions