Transparent Background For Modally Presented Viewcontroller

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

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.

Transparent View Controller Background When Presented Full Screen

When you present full screen the presenting view is removed from view - so there is nothing to see through your transparent view. So this method will not work for you.

When you present over the current context the presenting view remains - but that is why viewDidAppear etc do not get called - it never went away so it has not re-appeared.

Instead of viewDidAppear use another method of detecting that your transparent view has been dismissed, such as an unwind segue or one of the other methods discussed in this SO answer.

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

iOS : ModalView with background transparent?

you can check the iOS7 example (see my comm) or you can simple try this:

remove this line from your "presenting" method

controller.view.backgroundColor = [UIColor clearColor];

now, in viewDidLoad of the ShareController add:

 self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
self.modalPresentationStyle = UIModalPresentationFormSheet;

PS

if you have a navigationController... use

[self.navigationController presentViewController:controller animated:YES completion:nil];

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

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!

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.



Related Topics



Leave a reply



Submit