呈现一个透明的模态 UIViewController [英] Present a transparent modal UIViewController
问题描述
我正在尝试自己制作一个自定义的 alertView(适用于 iOS7+),但我在使用 alertView 演示文稿时遇到了困难.
I'm trying to make a custom alertView (for iOS7+) on my own but I struggle with the alertView presentation.
我有一个黑色背景的 UIViewController(alpha 设置为 0.25f),以及一个作为子视图的 alertView.
I have a UIViewController with a black background (alpha set to 0.25f), and a alertView as subview.
当我想显示 alertView 时,我以模态呈现 viewController:
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];
}
这是我的动画对象:
-(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 逐渐消失,但是当动画结束时,呈现的 VC 会从背景中移除.
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.
如果我调用 [transitionContext completeTransition:YES];
代替,呈现的 VC 在后台但模态 VC 在动画结束时被删除,所以我猜如果我们发送 '不'.
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'.
有没有办法让展示的 VC 保持在后台,而不必对其进行快照并将其设置为模态 VC 视图的背景?
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?
推荐答案
供您参考,我终于让我的自定义 alertView 成为弹出部分"的 UIView 的子类.为了显示它,我只是将 alertView 添加为 keyWindow 的子视图,并带有将其居中的约束,并在其后面放置一个透明的黑色背景视图.
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.
由于它不是控制器,我必须自己管理 UI 旋转(仅适用于 iOS 7,它与 iOS 8 中的 UI 旋转良好).
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).
这篇关于呈现一个透明的模态 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!