Present Uiviewcontroller as a Modal with Transparent Background

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.

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

Present a transparent modal UIViewController

For your information,
I finally made my custom alertView a subclass of UIView for the "popUp part".
To show it, I just add the alertView as subview of the keyWindow with the constraints to center it, and put a transparent black background view behind it.

As it's not a controller, I have to manage UI rotation by myself (only for iOS 7, it rotates well with the UI in iOS 8).

Present a modal view controller with transparent background

Suppose, we're in FirstViewController

//Obj-C
- (void) presentSecondVC {
SecondViewController *vc = [[SecondViewController alloc] init];
[self addChildViewController:vc];
[self didMoveToParentViewController:vc];
}

//Swift
func presentSecondVC() {
let vc = SecondViewController.init()
self.addChildViewController(vc)
self.didMove(toParentViewController: vc)
}

Some of you may need to write above method like this,

//Obj-C
- (void) presentSecondVC {
SecondViewController *vc = [[SecondViewController alloc] init];
vc.view.frame = CGRectMake(0,0,width,height); //Your own CGRect
[self.view addSubview:vc.view]; //If you don't want to show inside a specific view
[self addChildViewController:vc];
[self didMoveToParentViewController:vc];
//for someone, may need to do this.
//[self.navigationController addChildViewController:vc];
//[self.navigationController didMoveToParentViewController:vc];
}

//Swift
func presentSecondVC() {
let vc = SecondViewController.init()
vc.view.frame = CGRect.init(x: 0, y: 0, width: width, height: height) //Your own CGRect
self.view.addSubview(vc.view) //If you don't want to show inside a specific view.
self.addChildViewController(vc)
self.didMove(toParentViewController: vc)
//for someone, may need to do this.
//self.navigationController?.addChildViewController(vc)
//self.navigationController?.didMove(toParentViewController: vc)
}

Now in SecondViewController when you want to go back

//Obj-C
- (void) goBack {
[self removeFromParentViewController];
}

//Swift
func goBack() {
self.removeFromParentViewController()
}

Do play well (with each scenario) :)

And yes, this will not show an animation, in my case, I'm showing a custom popup inside vc though it looks nice with this code!

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.

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

Display clearColor UIViewController over UIViewController

RESOLVED: I fixed the issues. It is working so well for both of iPhone and iPad. Modal View Controller with no black background just clearColor/transparent. The only thing that I need to change is I replaced UIModalPresentationFullScreen to UIModalPresentationCurrentContext. How simple is that!

FirstViewController.m

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];

NOTICE: If you are using a modalPresentationStyle property of navigationController:

FirstViewController.m

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];

NOTICE: The bad news is that the above solution doesn't work on iOS 7. The good news is that I fixed the issue for iOS7! I asked somebody for help and here is what he said:

When presenting a view controller modally, iOS removes the view controllers underneath it from the view hierarchy for the duration it is presented. While the view of your modally presented view controller is transparent, there is nothing underneath it except the app window, which is black. iOS 7 introduced a new modal presentation style, UIModalPresentationCustom, that causes iOS not to remove the views underneath the presented view controller. However, in order to use this modal presentation style, you must provide your own transition delegate to handle the presentation and dismiss animations. This is outlined in the 'Custom Transitions Using View Controllers' talk from WWDC 2013 https://developer.apple.com/wwdc/videos/?id=218 which also covers how to implement your own transition delegate.

You may see my solution for the above issue in iOS7: https://github.com/hightech/iOS-7-Custom-ModalViewController-Transitions

Transparent Background with a Modal UIViewController

Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden.
See this previous question on the topic.

You can still use the view controller and NIB files you have set up - here's my sample code

- (void)showUpgrade {
[self.upgradeVC viewWillAppear:NO];
[self.view addSubview:self.upgradeVC.view];
[self.upgradeVC viewDidAppear:NO];
}

- (void)hideUpgrade {
[self.upgradeVC viewWillDisappear:NO];
[self.upgradeVC.view removeFromSuperview];
[self.upgradeVC viewDidDisappear:NO];
}

- (UpgradeViewController *)upgradeVC {
if (_upgradeVC == nil) {
_upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:@"UpgradeView_%@", self.deviceType] bundle:nil];
_upgradeVC.delegate = self;
}
return _upgradeVC;
}

You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.

It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.

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.



Related Topics



Leave a reply



Submit