Present a transparent modal UIViewController
Asked Answered
Q

6

15

I'm trying to make a custom alertView (for iOS7+) on my own but I struggle with the alertView presentation.

I have a UIViewController with a black background (alpha set to 0.25f), and a alertView as subview.

When I want to show the alertView, I present modally the viewController:

-(void) show 
{
    UIWindow* window = [[UIApplication sharedApplication] keyWindow];
    self.modalTransitionStyle = UIModalPresentationCustom;
    self.transitioningDelegate = self;
    [window.rootViewController presentViewController:self animated:YES completion:nil];
}

And here is my animator object:

-(NSTimeInterval) transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
    return 2;
}

-(void) animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    NSLog(@"%s",__PRETTY_FUNCTION__);

    UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    toView.alpha = 0;

    UIView* container = [transitionContext containerView];
    [container addSubview:toView];

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toView.alpha = 0.5;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

The thing is: the modal VC is fading with the presenting VC in background as its supposed to do, but when the animation ends the presenting VC is removed from the background.

If I call [transitionContext completeTransition:YES]; instead, the presenting VC is in background but the modal VC is removed at animation end, so I guess the context cancels the presentation if we send 'NO'.

Is there a way to keep the presenting VC in background without having to make a snapshot of it and set it as background of the modal VC's view?

Quinque answered 12/8, 2014 at 9:28 Comment(2)
Possible duplicate of #22004467 ?Elsworth
vc.modalPresentationStyle = UIModalPresentationOverFullScreen;Connel
Q
1

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

Quinque answered 28/8, 2014 at 7:9 Comment(0)
W
42

I've tried this solution and it works on both iOS 7 and 8:

if ([[UIDevice currentDevice].systemVersion integerValue] >= 8)
{
    //For iOS 8
    presentingViewController.providesPresentationContextTransitionStyle = YES;
    presentingViewController.definesPresentationContext = YES;
    presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
    //For iOS 7
    presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}

Note: Be aware of the difference between 'presentingViewController' and 'presentedViewController'.

Whitaker answered 14/1, 2015 at 16:47 Comment(4)
How da hell you don't have any upvotes? This is the best answer everAeonian
For full screen experience (specially when presenting from UINavigationController) use presentedViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;Felodese
good answer. this works for me also. only thing we need to take care is presented and presenting view controllers.Hexachlorophene
I also had to add background color of presented viewcontroller with alpha property like : [self.view setBackgroundColor:[UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:0.4]];Shortsighted
S
5

iOS8+

For iOS8+ you can use below code snippet

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    
Shuffleboard answered 11/1, 2017 at 7:35 Comment(0)
A
2

My case might differ from yours, but the information might be useful for the conversation.

In Storyboard, I changed my segue's Presentation to state "Over Full Screen" and it did the trick.

Amritsar answered 22/9, 2015 at 10:25 Comment(0)
B
1

I think what you are seeing is the default behavior of iOS.

View controllers are not supposed to be non-opaque when presented as modal view controllers. iOS removes the underlaying view controller when the animation is complete, in order to speed up composition when the presented view controller is supposed to take up the entire screen. There is no reason to draw a view controller - which might be complex in it's view hierarchy - when it is not even visible on screen.

I think your only solution is to do a custom presentation.

Remark: I did not test this. But it goes something like this.

/* Create a window to hold the view controller. */
UIWindow *presenterWindow = [[UIWindow alloc] init];

/* Make the window transparent. */
presenterWindow.backgroundColor = [UIColor clearColor];
presenterWindow.opaque = NO;

/* Set the window rootViewController to the view controller you
   want to display as a modal. */
presenterWindow.rootViewController = myModalViewController;

/* Setup animation */
CGRect windowEndFrame = [UIScreen mainScreen].bounds;
CGRect windowStartFrame = windowEndFrame;

/* Adjust the start frame to appear from the bottom of the screen. */
windowStartFrame.origin.y = windowEndFrame.size.height;

/* Set the window start frame. */
presenterWindow.frame = windowStartFrame;

/* Put the window on screen. */
[presenterWindow makeKeyAndVisible];

/* Perform the animation. */
[UIView animateWithDuration:0.5
                      delay:.0
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     presenterWindow.frame = windowEndFrame;
                 }
                 completion:^(BOOL finished){
                     /* Your transition end code */
                 }];

This does however leave you with no option to use any of the presenting view controller logic build into UIViewController. You'll need to figure yourself, when the presented view controller is done, and then reverse the animation and remove the UIWindow from screen.

Berneicebernelle answered 12/8, 2014 at 10:2 Comment(1)
Looking at #11236867, and more precisely at github.com/hightech/…, i think this is supposed to work. And more important, at the beginning my alertView was just a view I added to the app's window, but the view wasn't rotating. So I wrapped it in a VC to help him handle rotation. I think rotation isn't handled with your solution.Quinque
D
1

The ViewController is not supposed to be transparent when you present it or push it. You can try adding it as subview. And for transition effect change its frame immediately after adding as subview. Make its frame somewhere outside the visible view and then animate it to change frame to visible view.

Hope this helps.

Deidradeidre answered 12/8, 2014 at 11:16 Comment(1)
If I add it as a subview of the windows it won't rotate with the UI, and if i add it as a subview of the top VC the transparent background won't cover all of the UI.Quinque
Q
1

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

Quinque answered 28/8, 2014 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.