首页 > 解决方案 > 模态视图控制器子视图

问题描述

我以模态方式(透明)呈现视图控制器,然后尝试将对话框添加为带有几个按钮的子视图。我希望这个子视图是纯色的,但是不透明,但不能让它工作。

我尝试将子视图的 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)
    }
}

标签: iosswiftviewcontroller

解决方案


问题是这一行:

    self.view.alpha = 0.6

这会影响alpha此视图及其所有子视图,包括您的对话框。您不能使对话框具有完全有效的不透明度,因为它继承了它的透明度self.view

您可能打算做的是提供self.view.backgroundColor一些透明度。所以不要让它纯粹.white;使其.white与一些较低的alpha值一起使用。


推荐阅读