ios - 模态视图控制器子视图
问题描述
我以模态方式(透明)呈现视图控制器,然后尝试将对话框添加为带有几个按钮的子视图。我希望这个子视图是纯色的,但是不透明,但不能让它工作。
我尝试将子视图的 alpha 设置为 1,但它并没有改变外观。
class GameOverViewController: UIViewController {
private let restart = UIButton(type: .custom)
private let mainmenu = UIButton(type: .custom)
override func viewDidLoad() {
//displays view controller modally
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.alpha = 0.6
self.modalPresentationStyle = .overCurrentContext
//add dialogue box
let dialoguebox = UIView(frame: CGRect(origin: CGPoint(x: self.view.frame.width / 2, y: self.view.frame.height / 2), size: CGSize(width: self.view.frame.width / 2, height: self.view.frame.height / 2)))
dialoguebox.backgroundColor = .red
dialoguebox.center = self.view.center
dialoguebox.alpha = 1
self.view.addSubview(dialoguebox)
}
}
解决方案
问题是这一行:
self.view.alpha = 0.6
这会影响alpha
此视图及其所有子视图,包括您的对话框。您不能使对话框具有完全有效的不透明度,因为它继承了它的透明度self.view
。
您可能打算做的是提供self.view.backgroundColor
一些透明度。所以不要让它纯粹.white
;使其.white
与一些较低的alpha值一起使用。
推荐阅读
- javascript - 将数组中的数据发布到谷歌电子表格
- java - 如何使用 JFileChooser 保存 XLS 文件?
- php - 如何将字符串和int比较为int?
- mysql - 从给定日期中提取年份?
- ansible - 是否可以使用 Ansible playbook 遍历节点机器上的多个文件并搜索 n 替换特定行
- javascript - 为什么在 javascript 中使用 if(!!obj){} 而不是 if(obj){}
- python - 将复杂的分层 JSON 转换为 CSV
- ruby - 无法更新 Gemfile
- bash - 是否可以将变量从 BASH 脚本传递到 Linux 上的 PWSH
- html - flex-direction:行不工作;它仍然显示为一列