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!

4

4 回答 4

8

代码片段

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

将改变整个应用导航栏的颜色。

只需将其放在 Appdelegate 的 didFinishLauncing 方法中即可。

于 2012-11-02T11:14:07.507 回答
7

从 iOS 5 开始,您可以直接使用 UINavigationBar

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html 自定义导航栏的外观

只需设置titleTextAttributes字典

于 2011-11-17T12:36:15.057 回答
0

对于 iOS7 及更高版本,解决方案如下:

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

将此代码粘贴到 AppDelegate (didFinishLaunching:) 方法中,一切正常。

于 2014-08-28T23:53:28.987 回答
-1

不要在方法中设置viewDidAppear,请在viewDidLoad方法中进行。

于 2011-11-17T12:36:01.937 回答