Objective-C:导航栏中的背景图像和标题

发布于 2024-10-04 03:49:27 字数 1011 浏览 5 评论 0原文

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

@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];

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

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

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

[navigationController.navigationBar setBackgroundImage]; 

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. Hide Navigation Bar BEFORE opening the next ViewController:

    [self.navigationController setNavigationBarHidden:YES];

  2. Show the Navigation Bar in the next ViewController:

    [self.navigationController setNavigationBarHidden:NO];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2024-10-11 03:49:27

我得到了它!

@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

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

I got it!

@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

See Background image for navigation view

谁与争疯 2024-10-11 03:49:27

在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]];

After IOS5 You should do this, for example in 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]];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文