104

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

enter image description here

I'm trying to make a modal Swift in a little different from normal: By clicking on a button, the ViewController is displayed (following modal type) on the screen, but with transparent background. Only the blue View with label will be displayed. When this ViewController is presented, it is with transparent background, but as soon as it completes the transition, it will stay with the black background. Already deactivated the opaque option, and tested some options, but nothing this troubleshooting.

Some can help me?

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

I'm starting with swift, and I'm still pretty lost with how to program in Xcode, I read an answer to a question that has the following code to solve this:

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

Where do I put this code?

  • See this: http://stackoverflow.com/questions/27669699/transparent-background-for-modally-presented-viewcontroller – joern Oct 13 '15 at 14:10
  • I must keep the segue between the button and the ViewController (modal)? because without follows presents nothing, with the following features black background yet. –  Oct 13 '15 at 14:58
  • Refer this : http://stackoverflow.com/questions/26598099/present-uiviewcontroller-as-a-modal-with-transparent-background?rq=1 – Rajamohan S Sep 01 '16 at 04:10

1 Answers1

273

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 Segue 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.

AmitaiB
  • 1,656
  • 1
  • 19
  • 19
joern
  • 27,354
  • 7
  • 90
  • 105
  • Are you working with storyboards? Please see my edited answer. I just tried it and it works – joern Oct 13 '15 at 15:32
  • After you edit, follow his instructions and it worked properly. Thank you so much! –  Oct 13 '15 at 15:35
  • No problem, glad it helped :-) – joern Oct 13 '15 at 15:37
  • 2
    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. – Crashalot Dec 21 '15 at 07:24
  • @Crashalot Thanks for your comment! I added your info to the question to make it more visible. – joern Dec 21 '15 at 10:30
  • 2
    Swift3 - slideMenuController.modalPresentationStyle = .overCurrentContext present(slideMenuController, animated: true, completion: nil) – iluvatar_GR Sep 18 '16 at 11:47
  • @Crashalot that was very crucial in my case . thanks – user2695433 Nov 26 '16 at 15:38
  • How can I make this work for navigationController ` let vc = NewRquestViewController() vc.modalPresentationStyle = .overCurrentContext let nvc = UINavigationController(rootViewController: vc) self.present(nvc, animated: true, completion: nil)` – SwiftER Jan 18 '17 at 06:36
  • works like a charm! – NavodaP Nov 16 '17 at 09:26
  • 2
    Just to be really really precise as this is a fiddly one. In the storyboard its the *View controller* that needs to have Presentation set to 'Over Current Context', The *view* background colour as transparent (and untick opaque) – Derek Jan 25 '18 at 17:14
  • Can you make it show over a tabbar? I tried the method above and it displays over the previous VC, but under tabbar. – Senõr Ganso Aug 15 '18 at 10:01
  • 2
    Setting viewController.modalPresentationStyle = .overFullScreen solved it! – Senõr Ganso Aug 15 '18 at 10:03