0

I am presenting a view controller modally (transparent) and then trying to add a dialogue box as a subview with a few buttons. I would like this subview to be solid colour however and not transparent, but can't get it to work.

I have tried setting the alpha of the subview to 1 but it's not changing the appearance.

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)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
SanMu
  • 645
  • 1
  • 6
  • 19

1 Answers1

2

The problem is this line:

    self.view.alpha = 0.6

That affects the alpha of this view and all its subviews, including your dialog box. You cannot make the dialog have full effective opacity, because it inherits its transparency from self.view.

What you probably meant to do is give self.view.backgroundColor some transparency. So do not make it pure .white; make it .white along with some lower alpha value.

matt
  • 515,959
  • 87
  • 875
  • 1,141