Swift Modal View Controller with Transparent Background

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

iOS: Modal ViewController with transparent background

This following code only works on the iPad.

self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalVC animated:YES];

I would go with adding a sub view.

Here is a very good discussion. Look at the comments specifically. Not only the answer.

Modal View

If I were you I wouldn't do it. I would add a sub view and do it. It seems to give me a better control over things.

EDIT:

As mentioned by Paul Linsay, since iOS 8 all that's needed is UIModalPresentationOverFullScreen for the modalPresentationStyle of the ViewController being presented. This would also cover of navigationBar and tabBar buttons.

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.

Swift Modal View Controller with transparent background going beyond tab bar

It worked via vc.modalPresentationStyle = .overFullScreen

Present UIViewController as a modal with transparent background

For those still with this problem before presenting the UIViewController set the modalPresentationStyle of the presented UIViewController to .Custom and it will work on iOS 8(Xcode 6.1). That is, you should set it in the presenting UIViewController

How to use presentModalViewController to create a transparent view

Your view is still transparent, but once your modal controller is at the top of the stack, the view behind it is hidden (as is the case with any top-most view controller). The solution is to manually animate a view yourself; then the behind-viewController won't be hidden (since you won't have 'left' it).

Presenting a UIViewController with background transparent in swift 3.0

You just have to present your second Viewcontroller, which has a transparent background, modally. with the following config:

Sample Image

and that's the result :

Sample Image

for those who may not familiar with Opacity: in the situation like this, for transparency, it's better to use Opacity instead of Alpha

Sample Image



Related Topics



Leave a reply



Submit