setNavigationBarHidden在setViewControllers之后不起作用

时间:2017-03-31 12:04:06

标签: ios swift uinavigationcontroller uinavigationbar

我发现setNavigationBarHidden之后setViewControllers无法正常工作?

这是我的代码:

  • HomeNavController.swift

    class HomeNavController: UINavigationController {
    
        weak var tabBar: HomeTabBar!
    
        var tab: HomeTab = .match {
            didSet {
                switch self.tab {
                case .match:
                    self.setViewControllers([MatchViewController()], animated: false)
                case .moments:
                    self.setViewControllers([MomentsViewController()], animated: false)
                case .myPosts:
                    self.setViewControllers([PostsViewController()], animated: false)
                }
            }
        }
    }
    
  • MatchViewController.swift

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    

当我第一次切换到MatchViewController实例时,它运行良好,但在我使用func HomeNavController更改setViewControllers实例的viewControllers之后,它不起作用。< / p>

UINavigationController的错误吗?

1 个答案:

答案 0 :(得分:0)

也许尝试设置属性,这在我的情况下起作用:

    let navigationBarAppearance = UINavigationBar.appearance()
    let transparentColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0);
    // Sets the translucent background color
    navigationBarAppearance.backgroundColor = transparentColor
    navigationBarAppearance.titleTextAttributes = [NSForegroundColorAttributeName:transparentColor]
    // Hides the border
    navigationBarAppearance.shadowImage = UIImage()
    navigationBarAppearance.setBackgroundImage(UIImage(), for: .default)