Objective-C:导航栏中的背景图片和标题 [英] Objective-C: Background image and title in Navigation Bar

查看:213
本文介绍了Objective-C:导航栏中的背景图片和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In need a background image AND a title in my Navigation Bar. For the image I write a category:

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

我在AppDelegate中将此类别称为类别",并且在整个应用程序中都有背景图片:

I call this category in my AppDelegate and have background images in the whole application:

[navigationController.navigationBar setBackgroundImage]; 

每个ViewController都有标题:

Every ViewController has a title:

[self setTitle:@"MyTitle"];

但是设置背景图像后,标题出现了问题.

But after setting the background image, I have a problem with the title.

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

In the first view every works, I see the background image and the title :-) But in the next view, the title disappears. Only the background image is visible. Maybe the title is under the image?

从技术上讲,可以同时显示两者.有了这个技巧,它就可以工作了:

Technically it's possible to show both. With this trick it works:

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

  1. Hide Navigation Bar BEFORE opening the next ViewController:

[self.navigationController setNavigationBarHidden:YES];

[self.navigationController setNavigationBarHidden:YES];

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

Show the Navigation Bar in the next ViewController:

[self.navigationController setNavigationBarHidden:NO];

[self.navigationController setNavigationBarHidden:NO];

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

Now, image AND title are visible, but this solution isn't the best ;-)

推荐答案

我明白了!

@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

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

这篇关于Objective-C:导航栏中的背景图片和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆