我正在我的主viewcontroller上显示一个自定义弹出窗口。为此,我在故事板中创建了一个viewcontroller(显示的图像),相应的类如下所示。
class PopUpViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
self.showAnimate()
}
func showAnimate()
{
self.view.alpha = 1.0
}
func removeAnimate()
{
UIView.animate(withDuration: 0.0, animations: {
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished) {
self.view.removeFromSuperview()
}
});
}
}
然后在我的主视图控制器中,我按下按钮点击弹出如下:
let popOverVC = UIStoryboard(name: "MainViewController", bundle: nil).instantiateViewController(withIdentifier: "popup") as! PopUpViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.bounds
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
当添加弹出窗口时,这会使主视图控制器的背景变为黑色,不透明度为70%。如何使导航栏也具有相同的背景效果?
我试过更新:
self.view.window?.backgroundColor = UIColor.black.withAlphaComponent(0.7)
和
self.navigationController?.navigationBar.backgroundColor = UIColor.black.withAlphaComponent(0.7)
在viewDidLoad()但没有工作。任何可能的解决方
如果我理解正确,你将把popOverVC
作为子视图添加到mainViewController
中嵌入的UINavitationController
视图中。如果是这种情况,那么popOverVC
不会覆盖navigationBar是合乎逻辑的,因为navigationBar是navigationController
的子视图,而不是你的mainViewController
的子视图。为了能够覆盖navigationBar,你必须将popOverVC
添加到navigationController
:
// to make things a bit easier working with the optional self.navigationController
guard let navController = self.navigationController else { return }
let popOverVC = UIStoryboard(name: "MainViewController", bundle: nil).instantiateViewController(withIdentifier: "popup") as! PopUpViewController
navController.addChildViewController(popOverVC)
popOverVC.view.frame = navController.view.bounds
navController.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: navController)