iOS: Modal ViewController with transparent background
Asked Answered
S

24

199

I'm trying to present a view controller modally, with a transparent background. My goal is to let both the presenting and presented view controllers's view to be displayed at the same time. The problem is, when the presenting animation finishes, the presenting view controller's view disappears.

- (IBAction)pushModalViewControllerButtonPressed:(id)sender
{
    ModalViewController *modalVC = [[ModalViewController alloc] init];
    [self presentViewController:modalVC animated:YES completion:nil];
}

I know I could just add the view as a subview, but I'd like to avoid this solution for some reason. How could I fix it?

Sennet answered 5/10, 2012 at 7:4 Comment(6)
@TheKing Sounds like Michael, like me, wants the modal view to be translucent, to appear as a hovering gel layer over primary (presenting) view. Creates the sense of the user staying in the current context while making a quick setting, as opposed to going off to some other major functionality (in a separate view).Bonucci
This https://mcmap.net/q/129737/-present-a-modal-view-controller-with-transparent-background/1603234 make me smile, now your turn :)Youngman
See my answer here https://mcmap.net/q/129738/-ios-modalview-with-background-transparent for iOS 8Kotz
swift version here https://mcmap.net/q/129739/-display-clearcolor-uiviewcontroller-over-uiviewcontrollerEmmanuelemmeline
For iOS 10 is working UIModalPresentationOverFullScreen as inigo333 mentioned below.Garrick
vc.modalPresentationStyle = UIModalPresentationOverFullScreen;Nogas
R
107

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.

Retrusion answered 5/10, 2012 at 7:12 Comment(5)
It does work on iOS 7 and also on iPhone, the thing is that you have to specify the modalPresentationStyle = UIModalPresentationCurrentContext on the presenter view controller, and not in the presented one.License
Yes it works but ONLY if you set this on the higher ViewController of your view hierarchy. (eg. a NavigationController or the instance of your slide menu view controller for example).Lumpkin
Strangely, in my case it only worked after setting the modal style to UIModalPresentationCustom, and only if it's set right before presentViewController rather than in viewDidLoad.Bellew
Since iOS 8 all that's needed is UIModalPresentationOverFullScreen for the modalPresentationStyle of the ViewController being presented.Uranie
After about 1000 attempts, I realized you need to set modalPresentationStyle before viewDidLoad. I did it in the constructor and it worked.Galatians
P
193

For those trying to get this to work in iOS 8, the "Apple-approved" way to display a transparent modal view controller is by setting modalPresentationStyle on the presented controller to UIModalPresentationOverCurrentContext.

This can be done in code, or by setting the properties of the segue in the storyboard.

From the UIViewController documentation:

UIModalPresentationOverCurrentContext

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.

When presenting a view controller in a popover, this presentation style is supported only if the transition style is UIModalTransitionStyleCoverVertical. Attempting to use a different transition style triggers an exception. However, you may use other transition styles (except the partial curl transition) if the parent view controller is not in a popover.

Available in iOS 8.0 and later.

https://developer.apple.com/documentation/uikit/uiviewcontroller

The 'View Controller Advancements in iOS 8' video from WWDC 2014 goes into this in some detail.

Note:

  • Be sure to give your presented view controller a clear background color, lest it not actually be see-through!
  • You have to set this before presenting ie setting this parameter in the viewDidLoad of the presentedViewController won't have any affect
Purificator answered 23/9, 2014 at 8:9 Comment(7)
Note - It appears the target where you need to set modalPresentationStyle has changed. For example, in iOS 7, to get this to work, I needed to set the modalPresentationStyle of the View Controller(s) which was attempting to open the modal to UIModalPresentationCurrentContext. However, to get this to work in iOS8, I needed to set the modalPresentationStyle of the modal view which going to be displayed to UIModalPresentationOverCurrentContext.Stefa
If using segues in your storyboards, you have to set the presentation style there. At least this is what worked for me.Osanna
I added these three lines in the init method in the presented view controller, and works like a charm: self.providesPresentationContextTransitionStyle = YES; self.definesPresentationContext = YES; [self setModalPresentationStyle:UIModalPresentationOverCurrentContext];Sartor
On iOS 8+, I had to present a modal from a UINavigationController so presenting from a child did not provide me what I needed. Instead, sourceVC is self.navigationController. Also, only after setting the target presentation style as custom, I could see through it. [sourceVC setModalPresentationStyle:UIModalPresentationCurrentContext];, [targetVC setModalPresentationStyle:UIModalPresentationCustom]; Hopefully it will help someone.Etz
Note - Call presentedVC.modalPresentationStyle = UIModalPresentationOverCurrentContext in the presenting VC. Doesn't work in the presentedVC. Trust me, I tried.Novelia
To make presented controller's view call layoutSubviews (on change orientations for ex.) use 'UIModalPresentationOverFullScreen' instead of 'UIModalPresentationOverCurrentContext'.Dagney
@Etz 's answer helped if your source VC is a UINavigationController or UITabBarController. Great tip.Crabstick
G
110

In iOS 8.0 and above it can be done by setting the property modalPresentationStyle to UIModalPresentationOverCurrentContext

//Set property **definesPresentationContext** YES to avoid presenting over presenting-viewController's navigation bar

self.definesPresentationContext = YES; //self is presenting view controller
presentedController.view.backgroundColor = [YOUR_COLOR with alpha OR clearColor]
presentedController.modalPresentationStyle = UIModalPresentationOverCurrentContext;

[self presentViewController:presentedController animated:YES completion:nil];

See Image Attached

Gradate answered 12/1, 2015 at 19:40 Comment(6)
This is the only solution I could get to work. Very simple as well. Just add this to your presenting VC before the modal segue.Perusse
Xcoe 9.2/ iOS 11.2 swift .custom and .overFullScreen work.Udella
And seems if you set .overFullScreen the present view controller's viewWillAppear won't be called.Udella
One more thing, presentedController.view.backgroundColor = #color# should be written in presentedController's viewDidLoad, or else presentedController's life is interrupted.Briones
Coming to this thread in 2021 with Xcode 12.2 and iOS 14.2, this is the only solution that worked.Cavernous
Thank you so much! This is the solution I was looking for.Fluky
R
107

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.

Retrusion answered 5/10, 2012 at 7:12 Comment(5)
It does work on iOS 7 and also on iPhone, the thing is that you have to specify the modalPresentationStyle = UIModalPresentationCurrentContext on the presenter view controller, and not in the presented one.License
Yes it works but ONLY if you set this on the higher ViewController of your view hierarchy. (eg. a NavigationController or the instance of your slide menu view controller for example).Lumpkin
Strangely, in my case it only worked after setting the modal style to UIModalPresentationCustom, and only if it's set right before presentViewController rather than in viewDidLoad.Bellew
Since iOS 8 all that's needed is UIModalPresentationOverFullScreen for the modalPresentationStyle of the ViewController being presented.Uranie
After about 1000 attempts, I realized you need to set modalPresentationStyle before viewDidLoad. I did it in the constructor and it worked.Galatians
G
44

This code works fine on iPhone under iOS6 and iOS7:

presentedVC.view.backgroundColor = YOUR_COLOR; // can be with 'alpha'
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:YES completion:NULL];

In this case you miss slide-on animation. To retain animation you still can use the following "non-elegant" extension:

[presentingVC presentViewController:presentedVC animated:YES completion:^{
    [presentedVC dismissViewControllerAnimated:NO completion:^{
        presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
        [presentingVC presentViewController:presentedVC animated:NO completion:NULL];
    }];
}];

If our presentingV is located inside of UINavigationController or UITabbarController you need to operate with that controllers as presentingVC.

Further, in iOS7 you can implement custom transition animation applying UIViewControllerTransitioningDelegate protocol. Of course, in this case you can get transparent background

@interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>

First, before presenting you have to set modalPresentationStyle

modalViewController.modalPresentationStyle = UIModalPresentationCustom;

Then you have to implement two protocol methods

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    CustomAnimatedTransitioning *transitioning = [CustomAnimatedTransitioning new];
    transitioning.presenting = YES;
    return transitioning;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    CustomAnimatedTransitioning * transitioning = [CustomAnimatedTransitioning new];
    transitioning.presenting = NO;
    return transitioning;
}

The last thing is to define your custom transition in CustomAnimatedTransitioning class

@interface CustomAnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic) BOOL presenting;
@end

@implementation CurrentContextTransitionAnimator

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{
    return 0.25;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext 
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    if (self.presenting) {
        // custom presenting animation
    }
    else {
        // custom dismissing animation
    }
}
Godgiven answered 22/9, 2013 at 23:16 Comment(18)
Confirmed this works. NOTE that if your viewController is inside a navigationController then ensure its modalPresentationStyle is set also!Dantedanton
I think that in the most common cases it is good idea to treat your navigation controller as presentingVC.Godgiven
Yes, using this modalPresentationStyle impossible to get presenting animation ((Godgiven
i have the down animation onlyBalmy
Also the rotation is messed.Balmy
Nevertheless it works :) It seems that Apple didn't care about such case.Godgiven
Are you sure you set YOUR_COLOR properly? For example, presentedVC.view.backgroundColor = [UIColor clearColor]; ?Godgiven
@alex yes it works sorry: my mistake was to set modalPresentationStyle to UIModalPresentationCurrentContext for the presentedVC instead of the prenstingVC.Ly
@Dantedanton And if your viewController's navigationController is itself inside a tabBarController then ensure its modalPresentationStyle is set also ;)Erne
@user623396 write your iOS version. It perfectly works on iOS7 for example. Be sure you use "presentedVC" and "presentingVC" correctly.Godgiven
I added a new answer with a quick animation solution.Encrinite
It wasn't working for me because the presentingVC was in a navigation stack. Setting UIModalPresentationCurrentContext to the navigationViewController helped.Vigilantism
@user3099609, I forgot to mention the fact that in many cases presentingVC is actually could be navigation controller or tabbar controller.Godgiven
@Ly You have an "Embed Navigation Controller" right? You have to change the presentingVC to presentingVC.navigationViewControllerHeadwaters
@ThomasC.G.deVilhena And how to set modelPresentationStyle of tabBarController? Because seems to be no such method.Keepsake
@ChanchalRaj , modelPresentationStyle is the property of UIViewController which is the superclass of UITabBarController.Godgiven
Struggled long time to use the animation. your solution get me out of the problem. Thanks.Homburg
Guys, I wrote the same code, and it doesn't work for me. I have trying for a week now, but no success. Can anyone please help? func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool { self.presentedViewController?.view.backgroundColor = UIColor.redColor() self.presentingViewController?.tabBarController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext return true; }Competition
T
21

I struggled a bit with the Interface Builder of XCode 7 to set the Presentation Style as @VenuGopalTewari suggested. In this version, there seems to be no Over Current Context or Over Full Screen presentation mode for the segue. Thus, to make it work, I set the mode to Default:

enter image description here with enter image description here

Additionally I set the presentation mode of the modally presented view controller to Over Full Screen:

enter image description here

Tengdin answered 14/10, 2015 at 10:26 Comment(5)
This worked for me, no code needed. I am using Xcode 7.2, swift and the target is iOS 8.0Stigmatize
This worked for me too. +1 for the correct answer. Thanks :)Sergiosergipe
Best answer here !Lunde
No code = Best answer. Bastian wins this game. Plz all +1 this answer.Awaken
This answer worked..spending more than a hour on thisGe
H
20

The solution to this answer using swift would be as follows.

let vc = MyViewController()
vc.view.backgroundColor = UIColor.clear // or whatever color.
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)
Headrail answered 6/7, 2017 at 16:21 Comment(0)
T
19

Create a segue to present modally and set Presentation property of that segue to over current context it will work 100 %

enter image description here

Tammany answered 21/1, 2015 at 18:38 Comment(3)
I admire the 100% surers, it worked! The key is to apply it on the segue. Thank you a lotKlaipeda
If you don't have your segue hooked up to a button and you're calling it programmatically then make sure to set an identifier and to call performSegueWithIdentifier instead of presentViewControllerDealfish
I tried all the other answers, but this is the only one that worked for me in iOS9.Brigitte
B
15

PresentViewController with Transparent background - in iOS 8 and iOS 9

MYViewController *myVC = [self.storyboard   instantiateViewControllerWithIdentifier:@"MYViewController"];
    myVC.providesPresentationContextTransitionStyle = YES;
    myVC.definesPresentationContext = YES;
    [myVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    [self.navigationController presentViewController:myVC animated:YES completion:nil];

And in MYViewController set background color black and reduce opacity

Battleplane answered 23/6, 2016 at 8:6 Comment(0)
Z
12

It's a bit of hacky way, but for me this code works (iOS 6):

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

[self presentViewController:self.signInViewController animated:YES completion:^{
    [self.signInViewController dismissViewControllerAnimated:NO completion:^{
        appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
        [self presentViewController:self.signInViewController animated:NO completion:nil];
        appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;

    }];
}];

This code works also on iPhone

Zanthoxylum answered 24/9, 2013 at 11:29 Comment(4)
Thanks - this is exactly what I needed - showing an initial "Help" screen on top of the main Home. Beautiful!Motoring
Today, I spend almost 3 hours browsing and searching for solution which works for both iOS7 and iOS8. Considering the fact this answer is 1.5 years old and I'm using ECSlidingViewController, this is the only solution which worked! Thanks @Mak.Sooty
Perfect. Also, if you don't care about the animation, the 3 lines in the inner completion work perfectly Thanks!Terrorist
after 3 hours spent without any success found this, cheers, wish i could up vote more :DSudor
B
11

This category worked for me (ios 7, 8 and 9)

H file

@interface UIViewController (navigation)
- (void) presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
@end

M file

@implementation UIViewController (navigation)
- (void)presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
    if(SYSTEM_VERSION_LESS_THAN(@"8.0")) {
        [self presentIOS7TransparentController:viewControllerToPresent withCompletion:completion];

    }else{
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
         [self presentViewController:viewControllerToPresent animated:YES completion:completion];
    }
}
-(void)presentIOS7TransparentController:(UIViewController *)viewControllerToPresent withCompletion:(void(^)(void))completion
{
    UIViewController *presentingVC = self;
    UIViewController *root = self;
    while (root.parentViewController) {
        root = root.parentViewController;
    }
    UIModalPresentationStyle orginalStyle = root.modalPresentationStyle;
    root.modalPresentationStyle = UIModalPresentationCurrentContext;
    [presentingVC presentViewController:viewControllerToPresent animated:YES completion:^{
        root.modalPresentationStyle = orginalStyle;
    }];
}
@end
Breakfast answered 8/9, 2015 at 16:11 Comment(2)
I checked all the answer.. but for me your answer is perfect work.. "+1" for this. @BreakfastTannen
I thought I couldn't find a solution. Thanks a lot!Screens
D
11

If you're using Storyboard, you can follow this step:

  1. Add a view controller (V2), setup the UI the way you want it
  • add an UIView - set background to black and opacity to 0.5
  • add another UIView(2) - that will serve as your popup (Pls take note that the UIView and the UIView(2) must have the same level/hierarchy. Dont make the imageview the child of the view otherwise the opacity of the uiview will affect the UIView(2))
  1. Present V2 Modally

  2. Click the segue. In the Attributes inspector, Set Presentation as Over Full Screen. Remove animation if you like

Storyboard

  1. Select V2. In the Attributes inspector, Set Presentation as Over Full Screen. Check Defines Context and Provides Context

Storyboard

  1. Select the MainView of your V2 (Pls. Check image). Set backgroundColor to Clear Color

Storyboard

Decorative answered 20/9, 2017 at 3:24 Comment(0)
S
8

I added these three lines in the init method in the presented view controller, and works like a charm:

self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[self setModalPresentationStyle:UIModalPresentationOverCurrentContext];

EDIT (working on iOS 9.3):

self.modalPresentationStyle = UIModalPresentationOverFullScreen;

As per documentation:

UIModalPresentationOverFullScreen A view presentation style in which the presented view covers the screen. 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.

Available in iOS 8.0 and later.

Sartor answered 31/3, 2016 at 8:52 Comment(1)
Working on iOS 10.2.Garrick
T
4

Alternate way is to use a "container view". Just make alpha below 1 and embed with seque. XCode 5, target iOS7. Tested on iPhone.

enter image description here

Container view available from iOS6. Link to blog post about that.

Telmatelo answered 14/8, 2013 at 9:46 Comment(0)
V
4

I have created an object to handle the presentation of what I call a "superposed modal", meaning it retains the background's view and allows you to have a modal with a transparent background.

It has a single, simple method that does this:

- (void)presentViewController:(UIViewController *)presentedViewController
       fromViewController:(UIViewController *)presentingViewController
{
    presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
    presentedViewController.transitioningDelegate = self;
    presentedViewController.modalPresentationCapturesStatusBarAppearance = YES;

    [presentedViewController setNeedsStatusBarAppearanceUpdate];

    [presentingViewController presentViewController:presentedViewController
                                       animated:YES
                                     completion:nil];
}

It's important to set the modalPresentationCapturesStatusBarAppearance property to YES and force the status bar appearance to update, if your presented view controller has a different preferredStatusBarStyle.

This object should have a @property (assign, nonatommic) isPresenting

You want this object to comply to the UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate protocols and implement the following methods:

- (id)animationControllerForPresentedController:(UIViewController *)presented
                           presentingController:(UIViewController *)presenting
                               sourceController:(UIViewController *)source
{
    self.isPresenting = YES;

    return self;
}

- (id)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.isPresenting = NO;

    return self;
}

and:

- (NSTimeInterval)transitionDuration:(id)transitionContext
{
    return 0.25;
}

- (void)animateTransition:(id)transitionContext
{
    UIViewController* firstVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController* secondVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView* containerView = [transitionContext containerView];
    UIView* firstView = firstVC.view;
    UIView* secondView = secondVC.view;

    if (self.isPresenting) {
        [containerView addSubview:secondView];
        secondView.frame = (CGRect){
            containerView.frame.origin.x,
            containerView.frame.origin.y + containerView.frame.size.height,
            containerView.frame.size
        };

        firstView.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
        [UIView animateWithDuration:0.25 animations:^{
            secondView.frame = containerView.frame;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
        } else {
        [UIView animateWithDuration:0.25 animations:^{
            firstView.frame = (CGRect){
                containerView.frame.origin.x,
                containerView.frame.origin.y + containerView.frame.size.height,
                containerView.frame.size
        };

        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
}

This does a slide-in-from-the-bottom animation mimicking the default modal animation, but you can make it whatever you want.

The important thing is that the presenting view controller's view will remain in the back, letting you create a transparent effect.

This solution works for iOS 7+

Verve answered 27/11, 2014 at 16:52 Comment(1)
Thanks for Your Answer.Heavyfooted
P
3

A very simple way of doing this (using Storyboards, for example) is:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SomeStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SomeStoryboardViewController"];
// the key for what you're looking to do:
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
vc.view.alpha = 0.50f;

[self presentViewController:vc animated:YES completion:^{
    // great success
}];

This will present a UIViewController in a Storyboard modally, but with a translucent background.

Poole answered 14/1, 2016 at 16:19 Comment(0)
B
3

Working for iOS 7-10

if #available(iOS 8.0, *) {
    nextVC.modalPresentationStyle = .OverCurrentContext
    self.presentViewController(nextVC, animated: true, completion: nil)
} else {
    // Fallback on earlier version
    self.modalPresentationStyle = .Custom          
    nextVC.modalTransitionStyle = .CrossDissolve            
    self.presentViewController(nextVC, animated: false, completion: nil)
    }
}
Besom answered 15/3, 2017 at 12:42 Comment(0)
E
2

To recap all the good answers and comments here and to still have an animation while moving to your new ViewController this is what I did: (Supports iOS 6 and up)

If your using a UINavigationController \ UITabBarController this is the way to go:

    SomeViewController *vcThatWillBeDisplayed = [self.storyboard instantiateViewControllerWithIdentifier:@"SomeVC"];

    vcThatWillBeDisplayed.view.backgroundColor = [UIColor colorWithRed: 255/255.0 green:255/255.0 blue:255/255.0 alpha:0.50];    

    self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:presentedVC animated:YES completion:NULL];

If you'll do that you will lose your modalTransitionStyle animation. In order to solve it you can easily add to your SomeViewController class this:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.4 animations:^() {self.view.alpha = 1;}
       completion:^(BOOL finished){}];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.alpha = 0;
}
Encrinite answered 9/7, 2014 at 14:10 Comment(0)
T
2

Of course you should set UIModalPresentationCurrentContext, but the place to set clearColor is very important too! You can't set background in viewDidLoad function, set it before the view did load like in the root view controller or in the init function of the controller that going to present!

actionController.view.backgroundColor = [UIColor clearColor];
[self presentViewController:actionController animated:YES completion:nil];

or

- (instancetype)init {

    self = [super initWithNibName:nil bundle:nil];

    if(self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self.view setBackgroundColor:[UIColor clearColor]];
    }

    return self;
}
Toomer answered 6/12, 2018 at 2:14 Comment(1)
This was the hint that helped me among all the others. Thanks for your help.Fingerboard
B
2

Swift 4.2

guard let someVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "someVC") as? someVC else {
    return
}
someVC.modalPresentationStyle = .overCurrentContext

present(someVC, animated: true, completion: nil)
Broadbrim answered 15/4, 2019 at 19:14 Comment(0)
U
1

If you are using modal segue, make sure to set it as this image (you can turn off animation if you want)enter image description here

Utoaztecan answered 17/12, 2014 at 5:20 Comment(0)
S
1

A complete method tested on iOS 7 and iOS 8.

@interface UIViewController (MBOverCurrentContextModalPresenting)

/// @warning Some method of viewControllerToPresent will called twice before iOS 8, e.g. viewWillAppear:.
- (void)MBOverCurrentContextPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;

@end

@implementation UIViewController (MBOverCurrentContextModalPresenting)

- (void)MBOverCurrentContextPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    UIViewController *presentingVC = self;

    // iOS 8 before
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
        UIViewController *root = presentingVC;
        while (root.parentViewController) {
            root = root.parentViewController;
        }

        [presentingVC presentViewController:viewControllerToPresent animated:YES completion:^{
            [viewControllerToPresent dismissViewControllerAnimated:NO completion:^{
                UIModalPresentationStyle orginalStyle = root.modalPresentationStyle;
                if (orginalStyle != UIModalPresentationCurrentContext) {
                    root.modalPresentationStyle = UIModalPresentationCurrentContext;
                }
                [presentingVC presentViewController:viewControllerToPresent animated:NO completion:completion];
                if (orginalStyle != UIModalPresentationCurrentContext) {
                    root.modalPresentationStyle = orginalStyle;
                }
            }];
        }];
        return;
    }

    UIModalPresentationStyle orginalStyle = viewControllerToPresent.modalPresentationStyle;
    if (orginalStyle != UIModalPresentationOverCurrentContext) {
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    [presentingVC presentViewController:viewControllerToPresent animated:YES completion:completion];
    if (orginalStyle != UIModalPresentationOverCurrentContext) {
        viewControllerToPresent.modalPresentationStyle = orginalStyle;
    }
}

@end
Soul answered 10/3, 2015 at 6:26 Comment(0)
R
0

in appdelegate :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[_window rootViewController]setModalPresentationStyle:UIModalPresentationCurrentContext];
    return YES;
}

in you first view controller from where you have to load next view:

  NextViewController *customvc = [[NextViewController alloc]init];
    [self presentViewController:customvc animated:YES completion:^{

    }];

in your nextViewController which is to be added transparent:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UIView* backView = [[UIView alloc] initWithFrame:self.view.frame];
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    [self.view insertSubview:backView atIndex:0];
}
Reest answered 3/4, 2014 at 6:2 Comment(0)
H
0

The Login screen is a modal, meaning that it sits on top of the previous screen. So far we have Blurred Background, but it’s not blurring anything; it’s just a grey background.

We need to set our Modal properly.

image link target

  • First, we need to change the View Controller’s View background to Clear color. It simply means that it should be transparent. By default, that View is white.

  • Second, we need to select the Segue that leads to the Login screen, and in the Attribute Inspector, set the Presentation to Over Current Context. This option is only available with Auto Layout and Size Classes enabled.

image link target

Hesta answered 15/4, 2015 at 7:5 Comment(0)
S
0

Set navigation's modalPresentationStyle to UIModalPresentationCustom

and set your presented view controller's background color as clear color.

Semiskilled answered 5/7, 2017 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.