1

我的导航栏中需要背景图片和标题。对于图像,我写了一个类别:

@implementation UINavigationBar(MyNavigationBar)
- (void)setBackgroundImage {
    UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"navBarBackgrd.png"]];
    [self addSubview: aTabBarBackground];
    [self sendSubviewToBack: aTabBarBackground];
    [aTabBarBackground release];
}
@end

我在我的 AppDelegate 中调用了这个类别,并在整个应用程序中有背景图像:

[navigationController.navigationBar setBackgroundImage]; 

每个 ViewController 都有一个标题:

[self setTitle:@"MyTitle"];

但是设置背景图片后,标题出现了问题。

在每个作品的第一个视图中,我看到了背景图像和标题 :-) 但在下一个视图中,标题消失了。只有背景图像可见。也许标题在图像下方?

从技术上讲,两者都可以显示。有了这个技巧,它就起作用了:

  1. 在打开下一个 ViewController 之前隐藏导航栏:

    [self.navigationController setNavigationBarHidden:YES];

  2. 在下一个 ViewController 中显示导航栏:

    [self.navigationController setNavigationBarHidden:NO];

现在,图像和标题是可见的,但这个解决方案不是最好的 ;-)

4

2 回答 2

2

在 IOS5 之后你应该这样做,例如在 AppDelegate


 UIImage *img = [[UIImage imageNamed:@"image.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:0 green: 0 blue:0  alpha:1]];

于 2012-08-11T15:53:03.777 回答
2

我知道了!

@implementation UINavigationBar(MyNavigationBar)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"navBarBackgrd.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

请参阅导航视图的背景图像

于 2010-12-08T07:43:27.757 回答