我想让 View Controller 的 View 半透明。为此,我在 viewDidLoad
方法中设置了这样的背景颜色。
view.backgroundColor = UIColor(white: 0, alpha: 0.5)
当显示 View Controller 时,背景会根据我的需要出现,然后立即变黑。
为什么会这样?
这是显示 PopupViewController
的代码:
@IBAction func didTapShowButton(_ sender: UIButton) {
let navController = UINavigationController(rootViewController: PopupViewController())
present(navController, animated: true, completion: nil)
}
我上传了一个演示项目 here
最佳答案
您可以添加标志 overCurrentContext
(或 custom
),因此您的present
可能类似于:
@IBAction func didTapShowButton(_ sender: UIButton) {
let navController = UINavigationController(rootViewController: PopupViewController())
navController.modalPresentationStyle = .overCurrentContext
present(navController, animated: true, completion: nil)
}
关于ios - 透明 View 背景变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48044126/