2010-12-03 50 views
1

如何完成UINavigationBar自定义?它使用子类还是类?在福克斯新闻iPhone应用程序中自定义UINavigationBar

Fox news iphone app

我感兴趣的两个方面:

  1. 添加图像到的NavBar(如福克斯新闻标志)
  2. 自定义后退按钮 “显示”。 (后退按钮通常需要在堆栈中的前一视图的标题,但在前面的视图中没有标题。)

预先感谢任何帮助

回答

7

对于福克斯新闻应用程序,它看起来像他们举st设置导航栏的色调颜色。至于福克斯新闻标志,它可能只是导航栏标题视图上的图像视图。这个代码进入一个视图控制器的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 
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 
+0

我试过了,但是当navbar动画时(当新视图被按下或弹出时),图像不会动画。 – Shameem 2010-12-03 07:53:04

相关问题