Swift Modal View Controller with transparent background

I know this topic is quite popular, but I'm a little concerned about the problem in the programming language, the fact is that I still do not understand where I put the code. Ok, I will tell the whole case:

enter image description here

I am trying to make modal Swift a little different than usual. By clicking the button, the ViewController is displayed (after the modal type) on the screen, but with a transparent background. Only a blue view with a mark is displayed. When this ViewController is presented, it has a transparent background, but as soon as it completes the transition, it will remain on a black background. The opaque version has already been deactivated and some parameters have been tested, but nothing was found.

Can anybody help me?

Video is a test in a simulator on the case ( https://www.youtube.com/watch?v=wT8Uwmq9yqY ).

I start with the fast ones, and I still pretty much lost how to program in Xcode, I read the answer to the question that the following code has to solve this problem:

self.presentingViewController.providesPresentationContextTransitionStyle = YES; self.presentingViewController.definesPresentationContext = YES; modal.modalPresentationStyle = UIModalPresentationOverCurrentContext; 

Where can I put this code?

+63
ios swift swift2 modalviewcontroller
Oct 13 '15 at 14:01
source share
1 answer

You can do it as follows:

In the main view controller:

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

In your modular 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 Segue with Kind installed in Present Modally to your modal view controller, and set the following values ​​on this view controller:

  • Background = Clear Color
  • Drawing = Uncheck "Opaque"
  • Presentation = Current Context

As Crashalot pointed out in his comment: make sure that segue uses only Default for Presentation and Transition . Using Current Context for Presentation makes the modal rotation black, not transparent.

+157
Oct 13 '15 at 15:21
source share



All Articles