How to Use Presentmodalviewcontroller to Create a Transparent View

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

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

Presenting semi-transparent viewcontroller that works both in iOS7 and iOS8

You have to handle two configurations for iOS 7 and iOS8. In both case you need to make sure that the background view controller is not taken off the view hierarchy. This can be accomplish with:

  1. On iOS7 by setting the modalPresentationStyle flag to UIModalPresentationCurrentContext for the presenting view controller. You need to identify who is the presenting view controller: it is not necessarily self.navigationController if you have multiple container view controllers.

  2. On iOS8 by setting the modalPresentationStyle flag to UIModalPresentationOverCurrentContext for the presented view controller. If you are using storyboard make sure the presentation style is set to Default for the storyboard segue and within the prepareForSegue method set the presentation style to the destination view controller to UIModalPresentationOverCurrentContext.

Now to answer your corollary questions:

1) You need to have code to handle both situation on iOS7 and iOS8:

Define some macros to check the version number the application is running on. For example the following will do:

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

2) Obviously if you have code that needs to be compiled on iOS7 and iOS8 so your team needs to update to the latest version of XCode" XCode 6.1 as of now.

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.

Transparent ViewController to See Parent Below?

@Josh Kahane
set the view controller that will present the transparent view controller with this code at the -ViewDidLoad
and be sure to set the alpha channel of the UIViewController View to be lower then 1.

Code:

self.modalPresentationStyle = UIModalPresentationCurrentContext;

Presenting a view controller with transparency and animation

I ended up doing this:

AppDelegate *appDelegate = [AppDelegate sharedAppDelegate];

// Set the root VC modal presentation style
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;

WalkthroughViewController *walkthroughVC = [[WalkthroughViewController alloc] initWithNibName:nil bundle:nil];

[self presentViewController:walkthroughVC animated:NO completion:nil];

// Manually animate the view
walkthroughVC.view.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
walkthroughVC.view.alpha = 1;
}];

// Reset root VC modal presentation style
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;

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

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



Related Topics



Leave a reply



Submit