提出一个透明的模态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,所以如果我们发送'NO',我猜上下文会取消演示。
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成为popUp部分的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屋!