[英]Present a transparent modal 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];
}
这是我的动画师对象:
-(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将从背景中移除。
如果我调用[transitionContext completeTransition:YES];
相反,呈现VC在后台但模态VC在动画结束时被删除,所以我猜如果我们发送'NO',上下文会取消演示。
有没有办法将演示VC保留在后台而不必创建它的快照并将其设置为模态VC视图的背景?
我尝试过这个解决方案,它适用于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 '和' presentsViewController '之间的区别。
iOS8上+
对于iOS8 +,您可以使用下面的代码段
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:secondViewController animated:YES completion:nil];
我的情况可能与您的情况不同,但该信息可能对对话有用。
在故事板中,我将我的segue的演示文稿更改为“全屏幕”并且它完成了诀窍。
我认为你看到的是iOS的默认行为。
当呈现为模态视图控制器时,视图控制器不应该是非不透明的。 iOS在动画完成时删除了底层视图控制器,以便在呈现的视图控制器占据整个屏幕时加快合成速度。 没有理由在屏幕上甚至看不到视图控制器时 - 它的视图层次结构可能很复杂。
我认为您唯一的解决方案是进行自定义演示。
备注:我没有测试过这个。 但它就是这样的。
/* 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 */
}];
但是,这使您无法使用任何呈现视图控制器逻辑构建到UIViewController
。 完成呈现的视图控制器后,您需要自己计算,然后反转动画并从屏幕中删除UIWindow
。
当您呈现或推送它时,ViewController不应该是透明的。 您可以尝试将其添加为子视图。 并且对于过渡效果,在添加为子视图后立即更改其帧。 将其框架设置在可见视图之外的某个位置,然后设置其动画以将框架更改为可见视图。
希望这可以帮助。
为了您的信息,我终于使我的自定义alertView成为“popUp部分”的UIView的子类。 为了显示它,我只是将alertView添加为keyWindow的子视图,并使用约束来居中它,并在其后面放置一个透明的黑色背景视图。
由于它不是控制器,我必须自己管理UI旋转(仅适用于iOS 7,它可以在iOS 8中与UI一起旋转)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.