简体   繁体   English

隐藏导航栏但不隐藏后退按钮

[英]Hide Navigation Bar but not the back button

i hide my navigation using:我使用以下方式隐藏我的导航:

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

But i need to not hide the back button, it's Possible?但我不需要隐藏后退按钮,这可能吗?

nevan king is right but you can simply change the background image of the navigation bar or set it to nil. nevan king 是对的,但您可以简单地更改导航栏的背景图像或将其设置为 nil。 If you set it to nil or provide a transparent BG-image you would achieve the effect you need.如果将它设置为 nil 或提供透明的 BG 图像,您将获得所需的效果。

For iOS >= 5.0 you could simply set the appearance:对于 iOS >= 5.0,您可以简单地设置外观:

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

You can do that where ever you have a pointer to your navigation bar.只要有指向导航栏的指针,您就可以这样做。 Eg inside of the viewDidLoad method of your ViewController .例如,在ViewControllerviewDidLoad方法中。

For older iOS version you need a workaround by making a category of UINavigationBar and overwrite the drawRect method:对于较旧的 iOS 版本,您需要通过创建UINavigationBar类别并覆盖drawRect方法来解决问题:

@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

Both methods are compatible if you want to support all iOS versions.如果你想支持所有 iOS 版本,这两种方法都是兼容的。
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];

This code needs to be implemented for each ViewController you are pushing to your navigation bar.需要为您推送到导航栏的每个 ViewController 实施此代码。 A good place for it is also inside the viewDidLoad method.它的一个好地方也在viewDidLoad方法中。

The back button is created by the navigation bar and always part of it, so it's not possible.后退按钮由导航栏创建并且始终是其中的一部分,因此这是不可能的。 You could hide and re-show the navigation bar when your user touches on the screen (this is what the Photos app does when you look at a single photo) or create a button and have it permanently on the top left of the screen.您可以在用户触摸屏幕时隐藏并重新显示导航栏(这是照片应用程序在您查看单张照片时所做的)或创建一个按钮并将其永久放置在屏幕的左上角。 You could also make the navigation bar partly transparent so that the content underneath shows up.您还可以使导航栏部分透明,以便显示下面的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM