2017-02-21 59 views
0

我在一个NavigationController中嵌入了多个VC。 我有一个VC,让它命名为VCNotTransparent,我希望酒吧不透明,在其他VC我希望它是透明的。所以,现在Swift - 添加一个覆盖现有的导航条

 navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
     navigationController?.navigationBar.shadowImage = UIImage() 
     navigationController?.navigationBar.isTranslucent = true 

我所有的酒吧都在应用程序透明: 所以在主VC,我添加了这些线使酒吧透明。 如何在不更改所有其他VC的情况下使VCNotTransparent不透明?我想到的一个解决方案是只在VCNotTransparent中添加一个新的导航栏,但我不知道该怎么做。

编辑

我也尝试过在自己的NavigationController,其作品几乎嵌入VCNotTransparent,但问题是,我有导航从中其他一些VC的他们变得不透明为好,因为它们是子导航的VCNotTransparent。

回答

1

处理这种通过枚举 -

您MainVC内下方做 -

public enum NavigationType: Int { 
case transparent = 1 
case notTransparent = 2 
} 

var currentNavigationType: NavigationType? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.currentNavigationType = .transparent // default 
    self.setupNavigationControllerStyle() 
} 

func setupNavigationControllerStyle(){ 
switch self.currentNavigationType! { 
    case .transparent: 
     //do code here for transparent 
    case .notTransparent: 
     //do code here for not transparent 
    default: 
     break 
    } 

} 

默认情况下它会显示透明吧。在控制器,你不想透明条,只是从那里更新currentNavigationType财产像下面 -

class VCNotTransparent: MainVC { 

override func viewWillAppear(_ animated: Bool) { 
    self.currentNavigationType = .notTransparent 
    super.viewWillAppear(animated) 

    } 
} 
+0

感谢,这看起来像解决好的方向发展,我很感激。有一件事我没有注意到,因为我不明白什么是不透明的代码(在func setupNav中)。由于我使用了空白图像来使背景变得透明,我不知道如何将其恢复为不透明。 – Sharonica

+0

尝试,这可能是它会帮助你 - http://stackoverflow.com/questions/42332547/swift-3-how-to-reverse-a-transparent-navigation-bar –

+0

谢谢,它可以帮助一点,但它有一个问题,当透明条不透明时,条只会在延迟中变得透明(即使我在vc生命周期中将更改放入正确的函数中) – Sharonica