透明视图背景变黑

4

我希望将视图控制器的视图半透明。为此,我在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)
}

我也上传了一个演示项目这里


这可能是一个绝佳的机会,可以创建一个定制化的演示文稿,为您提供许多之前没有想到的设计思路。 - trndjc
1个回答

11
你可以添加标志overCurrentContext(或custom),这样你的present可能是这样的:
@IBAction func didTapShowButton(_ sender: UIButton) {
    let navController = UINavigationController(rootViewController: PopupViewController())
    navController.modalPresentationStyle = .overCurrentContext
    present(navController, animated: true, completion: nil)
}

也对我有用。Swift 4 - Naveed Ahmad
1
谢谢,非常好用!我在IOS 13中使用了这个方法,而不是isModalInPresentation = true,因为我的背景会变黑。 - Pedro Antonio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接