Restore navigationBar background image after setting it to [UIImage new]

可紊 提交于 2019-12-04 07:57:41

问题


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

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

That returns the desired effect, as seen here:

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

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


回答1:


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

Set this two method in your mapview

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:


For 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:


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.



来源:https://stackoverflow.com/questions/22090314/restore-navigationbar-background-image-after-setting-it-to-uiimage-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!