如何完成UINavigationBar自定义?它是使用子类还是类别?
我对两个方面感兴趣:
提前感谢您提供任何帮助
答案 0 :(得分:7)
对于Fox News应用程序,它们看起来只是设置导航栏的色调颜色。至于福克斯新闻标志,它可能只是导航栏标题视图上的图像视图。此代码进入视图控制器的viewDidLoad
方法:
[self.navigationController.navigationBar setTintColor:/* Custom color here */];
UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
[self.navigationItem setTitleView:logoView];
[logoView release];
要自定义后退按钮,您需要将其放在上一个视图控制器的viewDidLoad
方法中(即此按钮返回的方法):
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Shows"
style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
如果要为应用程序的导航栏使用完全自定义的背景图像,则需要创建自定义UINavigationBar
类别并在其drawRect:
方法中绘制图像。像这样:
@implementation UINavigationBar (UINavigationBarBackgroundImage)
- (void)drawRect:(CGRect)rect
{
[[UIImage imageNamed:@"navigation-bar"] drawInRect:rect];
// Optionally you can set the tintColor here to go with the background
}
@end
答案 1 :(得分:0)
将图像添加到导航栏使用此代码
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor]; //this use to set button color in FoxNew Site //button color
UIImage *img = [UIImage imageNamed: @"ImageName"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
为UINavigationBar创建类别
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
}
@end