【问题标题】:Modal viewcontroller subviews模态视图控制器子视图
【发布时间】:2018-12-20 20:13:50
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift viewcontroller


    【解决方案1】:

    问题出在这一行:

        self.view.alpha = 0.6
    

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

    您可能想要做的是给self.view.backgroundColor 一些透明度。所以不要让它纯粹.white;使它成为.white 以及一些较低的 alpha 值。

    【讨论】:

    • 有道理,工作,谢谢!之后一切似乎都那么明显...... oO
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多