在不同的视图控制器中更改NavigationBar标题(字体和颜色)

Sr.*_*hie 5 iphone uitableview navigationcontroller navigationbar

我试图在我的应用程序中自定义导航栏标题的外观.

我不得不构建一个自定义导航控制器(不只是为了这个问题),所以我想像这样覆盖setTitle函数

- (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];
}
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作.现在问题是在导航控制器内部我有一个TableView.单击一个单元格时,该应用程序向下钻取到另一个TableView,它应该再次具有自定义标题.

这是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];
}
Run Code Online (Sandbox Code Playgroud)

....但是当显示所选视图时,它会显示标准字体.

编辑 我注意到,如果在第二个视图中我在viewDidAppear方法中设置标题,它会发生不同的事情.如果我写

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

...导航栏中的标题具有正确的(自定义)字体,但标题就像仅在视图完成滑动时出现....?再次,我显然在这里做错了,任何帮助将不胜感激:)

感谢你的时间!

Des*_*ose 8

代码段

[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0/256.0 green:145.0/256.0 blue:35.0/256.0 alpha:1.0]]; 
Run Code Online (Sandbox Code Playgroud)

将改变整个应用程序的导航栏的颜色.

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


Mat*_*err 7

从iOS 5开始,您可以使用UINavigationBar直接执行此操作

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

只需设置titleTextAttributes字典


Ila*_*ian -1

请在方法中进行设置viewDidAppear,而不是在方法中进行设置viewDidLoad