Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift modal view background appearing black after setting it to clear

I am trying to present a custom view from a controller modally, however when I do so the background of everything but the textfield I have turns black as seen in here . (the intended modal)

This seems to be a fairly common problem, however I have already implemented the code that most similar questions pose as the answer

here is my addEvent method in the first controller:

@IBAction func addEvent(_ sender: Any) {
    let newEventViewController = NewEventViewController()
    newEventViewController.view.backgroundColor = UIColor.clear
    newEventViewController.view.isOpaque = false
    navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    navigationController?.present(newEventViewController, animated: true, completion: nil)
}

Any help figuring out what I am missing would be appreciated. If it makes a difference I am creating my app using individual xib files and not a storyboard.

like image 262
Christopher Konopka Avatar asked Dec 06 '25 08:12

Christopher Konopka


1 Answers

You need to set the modalPresentationStyle of newEventController not the navigationController like this:

newEventViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext

Also although not needed I would present from self rather than the navigation controller like this:

self.present(newEventViewController, animated: true, completion: nil)
like image 188
Upholder Of Truth Avatar answered Dec 08 '25 20:12

Upholder Of Truth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!