アプリでナビゲーション バーのタイトルの外観をカスタマイズしようとしていました。
(この問題だけでなく) カスタム ナビゲーション コントローラーを作成する必要があったため、次のように 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];
}
....しかし、選択したビューが表示されると、標準フォントで表示されます。
EDIT 2番目のビューでviewDidAppearメソッドでタイトルを設定すると、何か違うことが起こることに気付きました。私が書くなら
- (void)viewDidAppear:(BOOL)animated
{
self.navigationController.title = name;
[super viewDidAppear:animated];
}
...ナビゲーション バーのタイトルには適切な (カスタム) フォントがありますが、タイトルは、ビューがスライドインし終わったときにのみ表示されるようなものです....? 繰り返しますが、私は明らかにここで何か間違ったことをしています。助けていただければ幸いです:)
お時間いただきありがとうございます。