更改不同视图控制器中的导航栏标题(字体和颜色)
我试图在我的应用程序中自定义导航栏标题的外观。
我必须构建一个自定义导航控制器(不仅仅是为了这个问题),所以我想像这样重写 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,该 View 应该再次具有自定义标题。
这是 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];
}
...导航栏中的标题具有正确的(自定义)字体,但标题就像仅在视图完成滑入时才出现...? 再说一次,我显然在这里做错了什么,任何帮助将不胜感激:)
感谢您的时间!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代码片段
将改变整个应用程序导航栏的颜色。
只需将其放在 Appdelegate 的 didFinishLauncing 方法中即可。
The code snippet
Will change the color of the navigation bar of whole app.
Just place it in Appdelegate's didFinishLauncing method.
从 iOS 5 开始,您可以直接使用 UINavigationBar
https:// developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html
自定义导航栏的外观
只需设置
titleTextAttributes
字典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对于 iOS7 及更高版本,解决方案是这样的:
将此代码粘贴到 AppDelegate (didFinishLaunching:) 方法中,一切都应该正常工作。
For iOS7 and later the solution is this:
paste this code in the AppDelegate (didFinishLaunching:) method, and everything should work just fine.
请在
viewDidLoad
方法中进行设置,而不是在viewDidAppear
方法中进行设置。Instead of setting that in
viewDidAppear
method, please do that inviewDidLoad
method.