【发布时间】:2011-11-17 11:48:30
【问题描述】:
我试图在我的应用中自定义导航栏标题的外观。
我必须构建一个自定义导航控制器(不仅仅是为了这个问题),所以我想像这样覆盖 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];
}
一切都按预期进行。 现在的问题是在导航控制器内部我有一个 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];
}
....但是当所选视图出现时,它会以标准字体显示。
编辑 我注意到如果在第二个视图中我在 viewDidAppear 方法中设置标题,它会发生一些不同的事情。 如果我写
- (void)viewDidAppear:(BOOL)animated
{
self.navigationController.title = name;
[super viewDidAppear:animated];
}
...导航栏中的标题具有正确的(自定义)字体,但标题就像只有在视图完成滑入时才会出现......? 同样,我显然在这里做错了,任何帮助将不胜感激:)
感谢您的宝贵时间!
【问题讨论】:
标签: iphone uitableview navigationcontroller navigationbar