Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present a transparent modal UIViewController

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?

like image 270
Imotep Avatar asked Aug 12 '14 09:08

Imotep


People also ask

How do you make a ViewController transparent in Swift?

#1 Open the ViewController This would open the ViewController without any fancy animations, but we want moooore. So, to make the UI popover transparent, we need to set the modalPresentationStyle to . overFullScreen . And to avoid the blurry background swiping in from the bottom, we set the modalTransitionStyle to .

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

How do you make a modal view in Swift?

Go to the preview window en click the live view button, Press the “show modal” button to present the modal. A swipe down will dismiss the modal, but the “Dismiss” button can also be used.


2 Answers

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

like image 197
Basem Saadawy Avatar answered Sep 17 '22 01:09

Basem Saadawy


iOS8+

For iOS8+ you can use below code snippet

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    
like image 32
arunjos007 Avatar answered Sep 18 '22 01:09

arunjos007