模态 viewcontroller 子视图
Modal viewcontroller subviews
我正在以模态方式(透明)呈现一个视图控制器,然后尝试添加一个对话框作为带有几个按钮的子视图。我希望这个子视图是纯色而不是透明的,但无法让它工作。
我已经尝试将子视图的 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 值。
我正在以模态方式(透明)呈现一个视图控制器,然后尝试添加一个对话框作为带有几个按钮的子视图。我希望这个子视图是纯色而不是透明的,但无法让它工作。
我已经尝试将子视图的 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 值。