Swift - 添加一个覆盖现有导航栏的导航栏

标签 swift uinavigationcontroller

我在一个 NavigationController 中嵌入了多个 VC。 我有一个 VC,我们将其命名为 VCNotTransparent,我希望该栏不透明,而在其他 VC 上我希望它是透明的。 所以在主 VC 中,我添加了这些行来使栏透明:

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

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

编辑

我还尝试将 VCNotTransparent 嵌入到它自己的 NavigationController 中,这几乎可以工作,但问题是我有从它到其他一些 VC 的导航,它们也变得不透明,因为它们是 VCNotTransparent 的子导航。

最佳答案

通过枚举处理 -

在您的 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
    }

}

默认显示透明条。在哪个 Controller 中你不希望透明条只是从那里更新 currentNavigationType 属性,如下所示 -

class VCNotTransparent: MainVC {

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

   }
}

关于Swift - 添加一个覆盖现有导航栏的导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42366255/

相关文章:

arrays - Swift:从数组的结构中过滤字典键,这是可选的

ios - 从另一个 UIViewController 推送一个 UIViewController - 如何?

iOS 10 Beta 使导航栏按钮和标题在 pushViewController 上消失

ios - Metaio SDK : problems setting up a new project

ios - 由于 rootviewcontroller,在呈现新 View 后导航栏颜色发生变化

ios - 如何使用 swift 在 instagram 中构建用户个人资料页面?

ios - 无法设置模态视图 Controller 的导航栏颜色

ios - SpriteKit 中的攻击按钮

swift - 当我显示新的 UIView 时,UINavigationBar 失去色调颜色

ios - 无法关闭 CNContactViewController