透明视图背景变黑

时间:2017-12-31 16:33:29

标签: ios swift background transparency alpha-transparency

我想让视图控制器的视图半透明。为此,我在viewDidLoad方法中设置了这样的背景颜色。

view.backgroundColor = UIColor(white: 0, alpha: 0.5)

当显示视图控制器时,背景显示为我需要它,然后它立即变黑。

enter image description here

为什么会这样?

这是显示PopupViewController

的代码
@IBAction func didTapShowButton(_ sender: UIButton) {
    let navController = UINavigationController(rootViewController: PopupViewController())
    present(navController, animated: true, completion: nil)
}

我上传了一个演示项目here

1 个答案:

答案 0 :(得分:6)

您可以添加标记overCurrentContext(或custom),因此您的present可能类似于:

@IBAction func didTapShowButton(_ sender: UIButton) {
    let navController = UINavigationController(rootViewController: PopupViewController())
    navController.modalPresentationStyle = .overCurrentContext
    present(navController, animated: true, completion: nil)
}