2017-08-26 33 views
0

我已经从被推入的UIViewController中删除了UINavigationBar底线;UINavigationBar行在被推送的UIViewController上被移除后不会在父级UIViewController上显示

一切正常,直到我点击后退按钮,看到父母UIViewController(推谁)也没有底线。

这里的问题是我只想要从特定的UIViewController中删除底线,而不是从所有视图堆栈中移除底线。

这里是我如何删除行:

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

任何提示?

谢谢。

+0

我想你回到父视图控制器'viewWillAppear'方法时,你必须恢复导航栏的变化。 – Amit

+0

事情是,我怎么知道默认的背景图片?或者如何访问默认属性? –

回答

1

在父视图控制器的方法viewWillAppear添加:

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

或在视图控制器viewWillDisappear方法,其中,要更改的图像添加相同的上面的行。

0

你可以继承UINavigationController和改变图像在这样的willShowViewController(或didShowViewController)的委托方法:

class NavigationController: UINavigationController, UINavigationControllerDelegate { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     delegate = self 
    } 

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { 
     if viewControllers.count > 1 { 
      navigationBar.shadowImage = UIImage() 
      navigationBar.setBackgroundImage(UIImage(), for: .default) 
     } else { 
      navigationBar.shadowImage = nil 
      navigationBar.setBackgroundImage(nil, for: .default) 
     } 
    } 

} 

取决于你如何实例化导航控制器,你必须覆盖其他一些初始化阿尔斯好。

相关问题