将navigationBar背景图像设置为[UIImage new]后恢复

时间:2022-12-10 22:43:17

I needed a completely transparent navigation bar for the mapView so I did this:

我需要一个完全透明的mapView导航栏,所以我这样做:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];

That returns the desired effect, as seen here:

这会返回所需的效果,如下所示:

将navigationBar背景图像设置为[UIImage new]后恢复

Now I have a problem when I go to any other because my navigationBar remains transparent:

现在,当我去其他任何地方时我遇到了问题,因为我的navigationBar仍然是透明的:

将navigationBar背景图像设置为[UIImage new]后恢复

How do I restore default settings of the navigationBar's backgroundImage and shadowImage?

如何恢复navigationBar的backgroundImage和shadowImage的默认设置?

3 个解决方案

#1


46  

Set nil for image of navigation Controller on viewWillDisappear on map view

在地图视图上的viewWillDisappear上为导航控制器的图像设置nil

Set this two method in your mapview

在mapview中设置这两个方法

MapView.m

MapView.m

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

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

#2


5  

For Swift 3:

对于Swift 3:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)

  self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
  self.navigationController?.navigationBar.shadowImage = UIImage()
}

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)

  self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
  self.navigationController?.navigationBar.shadowImage = nil
}

#3


0  

by the way, you get get the original background image by using function

顺便说一句,你可以通过使用函数获得原始背景图像

UIImageView *imageView = [self.navigationController.navigationBar
backgroundImageForBarMetrics:UIBarMetricsDefault];

and store the image somewhere, then you use

并将图像存储在某处,然后使用

[self.navigationController.navigationBar setBackgroundImage:imageView 
forBarMetrics:UIBarMetricsDefault];

to set it back,but most time, set to nil will solve your problem.

将其设置回来,但大多数时候,设置为nil将解决您的问题。

#1


46  

Set nil for image of navigation Controller on viewWillDisappear on map view

在地图视图上的viewWillDisappear上为导航控制器的图像设置nil

Set this two method in your mapview

在mapview中设置这两个方法

MapView.m

MapView.m

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

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

#2


5  

For Swift 3:

对于Swift 3:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)

  self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
  self.navigationController?.navigationBar.shadowImage = UIImage()
}

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)

  self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
  self.navigationController?.navigationBar.shadowImage = nil
}

#3


0  

by the way, you get get the original background image by using function

顺便说一句,你可以通过使用函数获得原始背景图像

UIImageView *imageView = [self.navigationController.navigationBar
backgroundImageForBarMetrics:UIBarMetricsDefault];

and store the image somewhere, then you use

并将图像存储在某处,然后使用

[self.navigationController.navigationBar setBackgroundImage:imageView 
forBarMetrics:UIBarMetricsDefault];

to set it back,but most time, set to nil will solve your problem.

将其设置回来,但大多数时候,设置为nil将解决您的问题。