ios - 透明 View 背景变黑

标签 ios swift background transparency alpha-transparency

我想让 View Controller 的 View 半透明。为此,我在 viewDidLoad 方法中设置了这样的背景颜色。

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

当显示 View Controller 时,背景会根据我的需要出现,然后立即变黑。

enter image description here

为什么会这样?

这是显示 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/

相关文章:

Android,如何使用动画来显示闪烁?

ios - 从 SPM 包运行 SwiftUI 预览

ios - xcode Interface Builder 未更新 IBDesignable 类

ios - 如何使用 UIInputView 创建类似于按钮的键盘?

ios - 如何将两个 iOS 应用程序捆绑到一个 ipa 文件中

ios - 如何让我的动画功能 swift 依赖于 objective-c 麦克风打击检测?

swift - 在 do/catch block 中引用变量时使用未解析的标识符

java - 没有重复的视差滚动 |游戏

ios - 尝试在 Swift 3 中保存自定义对象时尝试插入非属性列表对象

javascript - 使用javascript结合背景css