我正在模态地呈现一个具有半透明视图的视图控制器。这是一个自定义活动指示器。我希望它覆盖视图,但保持导航栏和选项卡栏可见且可访问。
文档和几个 SO 答案(例如,呈现模态视图控制器隐藏导航栏)似乎表明,将模态呈现到导航控制器上应该可以实现这一点。但是当我这样做时,它会正确显示选项卡栏,但会覆盖导航栏。
有什么想法吗?以下是相关代码:
let spinnerVC = SpinnerViewController()
spinnerVC.modalPresentationStyle = .overCurrentContext
spinnerVC.modalTransitionStyle = .crossDissolve
self.navigationController?.present(spinnerVC, animated: true, completion: nil)
//self.navigationController is definitely not nil
您可以通过添加为navigationController
的rootViewController
来呈现您的viewcontroller
,然后在当前viewController
上呈现它,如下所示:
let spinnerVC = SpinnerViewController()
let navVC = UINavigationController(rootViewController:spinnerVC)
navVC.modalPresentationStyle = .overCurrentContext
navVC.modalTransitionStyle = .crossDissolve
self.present(navVC, animated: true, completion: nil)
您可以通过两种方式执行此操作: 第一个:
将此代码放在父视图中
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
第二个: 在警报控制器中添加对调用程序的引用,以便像这样隐藏栏:
weak var invokerView : UIViewController?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.invokerView?.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.invokerView?.navigationController?.setNavigationBarHidden(false, animated: animated)
}
不要呈现它。将其作为子视图控制器添加到导航控制器的顶部视图控制器,并将其视图作为子视图添加到同一调整框架的视图中。
let spinnerVC = SpinnerViewController()
spinnerVC.view.frame = self.navigationController?.topViewController?.view.bounds
self.navigationController?.topViewController?.addChildViewController(spinnerVC)
self.navigationController?.topViewController?.view.addSubview(spinnerVC.view)