Change NavigationBar Title (font and color) in different View Controllers
Asked Answered
T

4

5

I was trying to customize the look of the Navigation Bar Title in my app.

I had to build a Custom Navigation Controller (not just for this issue), so I thought 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 was working as expected. Now the issue is that inside a navigation controller I have a TableView. When clicking a cell, the app drills down to another TableView, which should have again the custom title.

this is the code for the 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 shows with the standard font.

EDIT I noticed that if in the second view I set the title in the viewDidAppear method, it happens something different. if I write

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

...the title in the navigation bar has the right (custom) font, but the title is like appearing only when the view has finished to slide in....? Again, I'm obviously doing something wrong here, any help would be appreciated :)

Thx for your time!

Tingaling answered 17/11, 2011 at 11:48 Comment(0)
D
8

The code snippet

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

Will change the color of the navigation bar of whole app.

Just place it in Appdelegate's didFinishLauncing method.

Demimonde answered 2/11, 2012 at 11:14 Comment(0)
B
7

Since iOS 5 you can do that directly with an UINavigationBar

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html Customizing the Appearance of a Navigation Bar

Just set the titleTextAttributes dictionary

Benge answered 17/11, 2011 at 12:36 Comment(3)
Thx for your reply, but the app must be 4.0 compatible :(Tingaling
So look at this post #599905 :-)Benge
Thanks again, I saw this post, you can find that I actually commented an answer and part of the code I wrote here is taken from there :) What actually puzzles me is that Erik B. told me "You should put it in all view controllers where you need to change the color of the title." But why the code in my navigation custom class is not triggered correctly? I'm an Objective-C noob :(Tingaling
A
0

For iOS7 and later the solution is this:

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

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

Almondeyed answered 28/8, 2014 at 23:53 Comment(0)
S
-1

Instead of setting that in viewDidAppear method, please do that in viewDidLoad method.

Slyviasm answered 17/11, 2011 at 12:36 Comment(1)
Thx for your reply, but if I set "self.navigationController.title = name" in the viewDidLoad method of the second view, when clicking the row the title changes in the first view BEFORE the second view slides inTingaling

© 2022 - 2024 — McMap. All rights reserved.