21

I would like to modally add a view controller with a transparent background, so the parent view controller beneath can be seen. (This is in an app for iPhone, not for iPad.)

I have tried this:

TextFieldViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"TextFieldVC"];
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentViewController:vc animated:YES completion:^{}];

Without luck and given the view controller a clear color background. My view controller is in a storyboard if that changes anything.

halfer
  • 19,824
  • 17
  • 99
  • 186
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

9 Answers9

15

@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;
Michal
  • 15,429
  • 10
  • 73
  • 104
orthehelper
  • 4,009
  • 10
  • 40
  • 67
  • 5
    This is what I thought, but I've tried this and my view just has a black background, no transparency. – Josh Kahane Dec 02 '12 at 21:55
  • 1
    man i am using this code to my augmented reality... again, write your mail i will send you an demo. – orthehelper Dec 03 '12 at 07:23
  • 3
    Well, after further investigating, if I do `self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;` then it works and my view is transparent. However it stops it from animating, any ideas? – Josh Kahane Dec 03 '12 at 11:18
  • it will animate only the Dismiss part... you didn't mention your working with navigation controller – orthehelper Dec 03 '12 at 11:41
  • I'm using MFSideMenu and assign my navigationController as the centerViewController and another UIViewController as the leftViewController. I tried assigning this value to self.window.rootviewcontroller in the appDelegate and also to self.menuContrainerViewController before I call presentViewController. Still black background! – ScorpionKing2k5 Nov 22 '13 at 09:09
  • 4
    strangely enough, if I do self.modalPresentationStyle = UIModalPresentationFormSheet in the viewDidLoad of the modally presented controller, it worked! – ScorpionKing2k5 Nov 22 '13 at 09:36
7

I have been searching for the solution. Now thanks to iOS 8. They has introduced couple of new modalPresentationStyle. one among them is UIModalPresentationOverCurrentContext. Used the same to solve the this issue.

viewcontroller.modalPresentationStyle = UIModalPresentationOverCurrentContext;

Hope this helps.

Michal
  • 15,429
  • 10
  • 73
  • 104
Abhijit
  • 384
  • 3
  • 12
7

For completeness and having it up-to-date, I am also adding the solution for Swift:

either

viewController.modalPresentationStyle = .CurrentContext 

being - A presentation style where the content is displayed over only the presenting view controller’s content.

or

viewController.modalPresentationStyle = .OverCurrentContext

being - A presentation style where the content is displayed over only the parent view controller’s content. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.

Depending on the requirements and circumstances for the presentation.

chourobin
  • 4,004
  • 4
  • 35
  • 48
Michal
  • 15,429
  • 10
  • 73
  • 104
6

It's possible although there are a bunch of steps which can be easily forgotten. After struggling with myself to get this working for 4 hours I came up with the following solution.

1 - Create a View Controller like any other.

Header file

#import "DDBaseViewController.h"

@interface DDHomeSearchLoadingViewController : UIViewController

@end

Implementation file

#import "DDHomeSearchLoadingViewController.h"

@interface DDHomeSearchLoadingViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityMonitor;
@property (weak, nonatomic) IBOutlet UIView *modalView;

@end

@implementation DDHomeSearchLoadingViewController

#pragma mark - UIViewController lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];


    [self setupUI];
}

-(void) setupUI
{
    [self makeRoundedCorner:self.modalView AndCornerRadius:6.0f];
    [self.activityMonitor startAnimating];

}

-(void) makeRoundedCorner:(UIView*) view AndCornerRadius:(float) cornerRadius
{
    [view.layer setCornerRadius:cornerRadius];
    [view.layer setMasksToBounds:YES];
}

@end

2 - Set your container view background color as ClearColor

enter image description here

3 - Add a UIView which will be presented like an overlay

enter image description here

4 - Add a UIView which will be presented like a Dialog Box over the overlay UIView

Make sure it's outside the overlay view when you add/move it. For some reason when you move it using the mouse it adds to overlay UIView automatically. ( Freaking annoying to be honest )

enter image description here

5 - Set the Storyboard ID for the View you've created

enter image description here

6 - Finally add this piece of code wherever you wan't call it ( Assuming it's a UiViewController too )

DDHomeSearchLoadingViewController* ddHomeSearchLoadingViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DDHomeSearchLoadingViewController"];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:ddHomeSearchLoadingViewController animated:YES completion:nil];

I hope it helps you guys out

Cheers

Paulo Miguel Almeida
  • 2,114
  • 31
  • 36
2

Answer for Swift 5.

TransparentViewController

self.view.backgroundColor = .clear

PresentingViewController

let viewController = TransparentViewController()
viewController.modalPresentationStyle = .overFullScreen
navigationController.present(viewController, animated: false)
Arthur Stepanov
  • 511
  • 7
  • 4
1

Swift 3 -

Try vc.modalPresentationStyle = .overFullScreen

let vc = self.storyboard!.instantiateViewControllerWithIdentifier("ExampleViewController") as! ExampleViewController
vc.view.backgroundColor = UIColor.clearColor()
vc.modalPresentationStyle = .overFullScreen
self.presentViewController(vc, animated: true, completion: nil)
Allen
  • 2,979
  • 1
  • 29
  • 34
0
UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
ChooseController *sec = [story instantiateViewControllerWithIdentifier:@"Controller"];
sec.modalPresentationStyle = UIModalPresentationOverCurrentContext;
sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:sec animated:YES completion:^{}];

Note: Present controller superview alpha value must be below 1 like 0.5 alpha like that.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
0

you can make changes in storyboard just by doing below options and also reducing parental view opacity. Using Storyboard: No need to write any code to achieve this

enter image description here

-2

When you modally present a view controller the , it goes on the stack which in turn hides the view controller below it. So all you can do is to present a transparent view with animation similar to modally presenting a view controller.

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115