呈现一个透明的模态UIViewController

我试图自己做一个自定义alertView(对于iOS7 +),但我挣扎alertView演示。

我有一个UIViewController与黑色背景(alpha设置为0.25f),并将alertView作为子视图。

当我想显示alertView时,我以模态方式呈现viewController:

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

这是我的animation师对象:

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

事情是:模态VC与背景中呈现的VC一样随着它应该做的而退色,但是当animation结束时,呈现VC从背景中移除。

如果我调用[transitionContext completeTransition:YES]; 相反,呈现的VC在背景中,但模式VC在animation结束时被移除,所以如果我们发送“否”,我猜上下文会取消呈现。

有没有办法让VC呈现在后台,而不需要做一个快照,并将其设置为模态VC视图的背景?

我已经尝试了这个解决scheme,它在iOS 7和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; } 

注意:要注意' presentsViewController '和' presentedViewController '之间的区别。

iOS8上+

对于iOS8 +,您可以使用下面的代码片段

 SecondViewController *secondViewController = [[SecondViewController alloc] init]; secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:secondViewController animated:YES completion:nil]; 

我的情况可能与您的情况有所不同,但是这些信息对于对话可能是有用的。

在Storyboard中,我改变了我的Segue的Presentation来陈述“Over Full Screen”,它实现了这个诀窍。

我认为你所看到的是iOS的默认行为。

视图控制器在呈现为模式视图控制器时不应该是不透明的。 当animation完成时,iOS会删除底层的视图控制器,以便在呈现的视图控制器占据整个屏幕时加快构图。 没有理由画一个视图控制器 – 当视图控制器在视图层次结构中可能是复杂的,甚至在屏幕上看不到的时候。

我认为你唯一的解决办法是做一个自定义的演示文稿。

备注:我没有testing这个。 但它是这样的。

 /* 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 */ }]; 

但是,这不会让你没有select使用任何呈现视图控制器逻辑build立到UIViewController 。 当呈现的视图控制器完成时,您将需要自己UIWindow ,然后反转animation并从屏幕中移除UIWindow

ViewController在呈现或推送时不应该是透明的。 你可以尝试添加它作为子视图。 对于过渡效果,在添加子视图之后立即改变其框架。 使其框架位于可见视图之外的某个位置,然后对其进行animation处理,将框架更改为可见视图。

希望这可以帮助。

为了您的信息,我终于使我的自定义alertView的“popup部分”的UIView的子类。 为了显示它,我只是将alertView作为keyWindow的子视图添加约束来将其居中,并在其后面放置一个透明的黑色背景视图。

因为它不是控制器,所以我必须自己pipe理UI旋转(仅适用于iOS 7,它可以在iOS 8中使用UI进行很好的旋转)。