Change the title of the NavigationBar (font and color) in different View controllers

I tried to customize the appearance of the navigation bar title in my application.

I had to create a custom navigation controller (not only for this problem), so I decided to override the setTitle function like this

- (void)setTitle:(NSString *)title
{
    NSLog(@"SETTING TITLE %@", title);
   [super setTitle:title];
   UILabel *titleView = (UILabel *)self.navigationBar.topItem.titleView;
   NSLog(@"title view is %@", titleView);
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont fontWithName:@"TradeGothicLTStd-Bold-Accent-Accent" size:20];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        titleView.textColor = [UIColor whiteColor];
        self.navigationBar.topItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = [title uppercaseString];
    [titleView sizeToFit];

    self.navigationBar.tintColor= [UIColor colorWithRed:130.0f/255.0f green:120.0f/255.0f blue:90.0f/255.0f alpha:1.0f];
}

Everything worked as expected. Now the problem is that inside the navigation controller I have a TableView. When you click on a cell, the application navigates to another TableView, which should again have a custom title.

this is the code for didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *category_id = [[managedObject valueForKey:@"category_id"] stringValue];
    NSString *name = [[[managedObject valueForKey:@"name"] description] capitalizedString];
    CategoryViewController *category = [[CategoryViewController alloc] initWithNibName:@"CategoryViewController" bundle:nil];
    category.category_id = category_id;
    category.name = name;
    category.managedObjectContext = self.managedObjectContext;
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:category animated:YES];
    [category release];
}

.... but when the selected view appears, it is displayed with a standard font.

, viewDidAppear, - .

- (void)viewDidAppear:(BOOL)animated
{
self.navigationController.title = name;
[super viewDidAppear:animated];
}

... () , , ....? , , , - , :)

!

+5
4

[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0/256.0 green:145.0/256.0 blue:35.0/256.0 alpha:1.0]]; 

.

Appdelegate didFinishLauncing.

+8

For iOS7 and later, this solution:

[UINavigationBar appearance].barTintColor = [UIColor redColor];

paste this code into the AppDelegate (didFinishLaunching :) method and everything should work fine.

0
source

Instead of setting it in a method viewDidAppear, do it in a method viewDidLoad.

-1
source

All Articles