隐藏导航栏,但不是后退按钮 [英] Hide Navigation Bar but not the back button

查看:206
本文介绍了隐藏导航栏,但不是后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我隐藏了我的导航:

[self.navigationController setNavigationBarHidden:YES animated:YES];

但我需要不隐藏后退按钮,这可能吗?

But i need to not hide the back button, it's Possible?

推荐答案

nevan king是正确的,您可以简单地更改导航栏的背景图片或将其设置为nil。

nevan king is right but you can simply change the background image of the navigation bar or set it to nil. If you set it to nil or provide a transparent BG-image you would achieve the effect you need.

对于iOS> = 5.0,你可以简单地设置外观:

For iOS >= 5.0 you could simply set the appearance:

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) // needed if iOS older than 5.0 is also supported
    [navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

您可以在任何时候有指向导航栏的指针。例如。在 ViewController viewDidLoad 方法内。

You can do that where ever you have a pointer to your navigation bar. E.g. inside of the viewDidLoad method of your ViewController.

对于较旧的iOS版本,您需要通过类别 UINavigationBar 覆盖 drawRect 方法:

For older iOS version you need a workaround by making a category of UINavigationBar and overwrite the drawRect method:

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

如果您要支持所有iOS版本。

因此,您应该记住,后退按钮使用相同的背景图片。因此您需要自定义一个。

Both methods are compatible if you want to support all iOS versions.
Thus you should keep in mind, that the back button uses the same background image. So you will need to make a custom one.

UIImage *bgImageNormal = [UIImage imageNamed:@"backButtonImage.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: bgImageNormal forState:UIControlStateNormal];

button.frame= CGRectMake(0.0, 0.0, bgImageNormal.size.width, bgImageNormal.size.height);
[button addTarget:self action:@selector(navigationBarBackButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; // your action method here

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = closeButton;
[closeButton release];

这些代码需要针对您推送到导航栏的每个ViewController实现。一个好的地方也在 viewDidLoad 方法内。

This code needs to be implemented for each ViewController you are pushing to your navigation bar. A good place for it is also inside the viewDidLoad method.

这篇关于隐藏导航栏,但不是后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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