简体   繁体   English

删除UINavigationBar上的背景图像

[英]Remove Background Image on UINavigationBar

My app uses a navigation controller with an image on in the navigation bar of the home page. 我的应用程序使用导航控制器,主页导航栏中有图像。 The background image is done by putting 背景图像是通过推杆完成的

[navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[_window addSubview:navigationController.view];

in the app delegate. 在应用程序委托中。 The nav bar displays this image fine. 导航栏显示此图像正常。

Every other view will not have an image in the navigation bar. 每个其他视图在导航栏中都没有图像。 Therefore, I need to remove this background image when any other view controller is pushed. 因此,我需要在按下任何其他视图控制器时删除此背景图像。 Does anyone know how this can be done? 有谁知道如何做到这一点?

There are many answers online about adding or changing a nav bar image, but this question is a little bit different. 在线有很多关于添加或更改导航栏图像的答案,但这个问题有点不同。 Thank you in advance. 先感谢您。

To remove UINavigationBar background in iOS 5 or later, you should do this: 要在iOS 5或更高版本中删除UINavigationBar背景,您应该这样做:

[navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

Hope it helps 希望能帮助到你

You can set background image with blank image created in runtime. 您可以使用在运行时创建的空白图像设置背景图像。

UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];

Creating blank image in runtime: How to create a blank transparent png in Objective-C? 在运行时创建空白图像: 如何在Objective-C中创建空白透明png?

I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. 我相信这是iOS5的第一个实际答案,主要问题是一旦完成后“删除”背景图像。 Well, just retain the existing image and put it back when you are done. 好吧,只需保留现有图像,并在完成后将其放回原处。

@implementation MyViewController {
    UIImage *_defaultImage;
}

- (void)viewWillAppear:(BOOL)animated {   
    _defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}

Nothing here have worked for me. 这里没有什么对我有用。 After searching for hours, the only thing that worked was following a source code example provided by Apple. 搜索了几个小时之后,唯一有用的是遵循Apple提供的源代码示例

I placed the following code and it worked and removed the navigation bar image from other view controllers finally!! 我放置了以下代码,它最终工作并从其他视图控制器中删除了导航栏图像!

// Reset everything about navigation bar. (Other flows' background image used to show up on another view controller)
    self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
    self.navigationController!.navigationBar.setBackgroundImage(nil, for: .compact)
    self.navigationController!.navigationBar.barTintColor = nil
    self.navigationController!.toolbar.barTintColor = nil
    self.navigationController!.toolbar.isTranslucent = true

Hope this helps someone. 希望这有助于某人。

Cheers 干杯

It looks like the easiest way to do this is to: 看起来最简单的方法是:

1) set the alpha of the navigation bar on the home page to 0.000001 1)将主页上导航栏的alpha设置为0.000001

2) put the background image in a UIImageView "behind" the navigation bar (which is now clear) 2)将背景图像放在UIImageView的“后面”导航栏(现在已清除)

3) use viewWillAppear / viewWillDisappear to set the navigation bar's opacity to either 1 or 0.00001 when views are pushed / popped. 3)使用viewWillAppear / viewWillDisappear在推送/弹出视图时将导航栏的不透明度设置为1或0.00001。

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

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