Transparent background for modally presented viewcontroller Transparent background for modally presented viewcontroller swift swift

Transparent background for modally presented viewcontroller


Fixed it.

The problem was that presentViewController does not keep the view that I was covering.

viewController.modalPresentationStyle = .overCurrentContext

did the trick.


Part of the solution is hidden in the question. You need three lines to make the background transparent, viz. isOpaque = false backgroundColor = .clear & set the modalPresentationStyle

Here's the full solution. In the calling View Controller, call this function:

func presentModal() {    let modalController = ModalViewController()    modalViewController.modalPresentationStyle = .overCurrentContext    present(modalViewController, animated: true, completion: nil)}

And in ModalViewController's viewDidLoad():

override func viewDidLoad() {    super.viewDidLoad()    view.isOpaque = false    view.backgroundColor = .clear // try other colors, say: .white or black with Alpha etc.}


same as the selected answer but visually through IB:

enter image description here