推送新的 viewcontroller 时,自定义导航栏子视图不会隐藏

Custom Navigation Bar Subview does not hide when a new viewcontroller is pushed

因此,在我的 HomeViewController 上,我添加了一个自定义导航栏 @IBOutlet weak var navBar: UIView!,并将其添加到导航栏中:

self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.backgroundColor = .clear
self.navigationController?.view.insertSubview(navBar, belowSubview: navigationController!.view)
self.navigationController?.navigationBar.isTranslucent = false
self.edgesForExtendedLayout = []

现在当我按下 MenuViewController

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MenuVC") as! MenuViewController
self.navigationController?.pushViewController(vc, animated: true)

这个习俗navBar还在最前面。我希望默认导航栏再次显示,因为我只想在 HomeViewController

上自定义 navBar

您应该在推送 MenuViewController 之前从 navigationController 中删除自定义视图,或者使用 MenuViewControllerviewWillAppear 中的标签访问自定义视图并将其从超级查看或隐藏它

  • 您已将自定义 navbar 添加到 navigationController.view, 这就是为什么它会出现在每个 viewController 上,其中 navigationController 是或将被使用。

你可以通过以下方式解决这个问题

  • navbar 添加到当前 UIView
  • 的子视图中

self.view.addSubview(navbar)

  • 从那个特定的 UIViewController
  • 中隐藏 navigationBar

self.navigationController?.setNavigationBarHidden(true, animated: false)

  • 展示给下一个ViewController

self.navigationController?.setNavigationBarHidden(false, animated: false)