Skip to content

Instantly share code, notes, and snippets.

@fahied
Forked from barbietunnie/modal-view.md
Created July 28, 2020 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahied/c50325ba82100f2915835ed4b3cc0c14 to your computer and use it in GitHub Desktop.
Save fahied/c50325ba82100f2915835ed4b3cc0c14 to your computer and use it in GitHub Desktop.
Swift Modal View Controller with transparent background

You can do it like this:

In your main view controller:

func showModal() {
    let modalViewController = ModalViewController()
    modalViewController.modalPresentationStyle = .OverCurrentContext
    presentViewController(modalViewController, animated: true, completion: nil)
}

In your modal view controller:

class ModalViewController: UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.clearColor()
        view.opaque = false
    }
}

If you are working with a storyboard:

Just add a Storyboard Seque with Kind set to Present Modally to your modal view controller and on this view controller set the following values:

Background = Clear Color
Drawing = Uncheck the Opaque checkbox
Presentation = Over Current Context

As Crashalot pointed out in his comment: Make sure the segue only uses Default for both Presentation and Transition. Using Current Context for Presentation makes the modal turn black instead of remaining transparent.

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment