提出一个透明的模态UIViewController

Imo*_*tep 14 uiviewcontroller modalviewcontroller ios custom-transition

我正在尝试自己制作一个自定义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];
}
Run Code Online (Sandbox Code Playgroud)

这是我的动画师对象:

-(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];
    }];
}
Run Code Online (Sandbox Code Playgroud)

事情是:模态VC在背景中呈现VC呈现淡化,但是当动画结束时,呈现VC将从背景中移除.

如果我[transitionContext completeTransition:YES];改为调用,则呈现VC在后台但是在动画结束时删除了模态VC,所以如果我们发送'NO',我猜上下文取消了演示.

有没有办法将演示VC保留在后台而不必创建它的快照并将其设置为模态VC视图的背景?

Bas*_*awy 38

我尝试过这个解决方案,它适用于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;
}
Run Code Online (Sandbox Code Playgroud)

注意:请注意' presentsViewController '和' presentsViewController '之间的区别.

  • 你怎么没有任何upvotes?这是有史以来最好的答案 (3认同)
  • 对于全屏体验(特别是在从`UINavigationController呈现时),使用`presentViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;` (2认同)

aru*_*007 5

iOS8上+

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

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    
Run Code Online (Sandbox Code Playgroud)


Imo*_*tep 1

供您参考,我最终将自定义的alertView 设为“popUp 部分”的 UIView 的子类。为了展示它,我只需将alertView添加为keyWindow的子视图,并带有使其居中的约束,并在其后面放置一个透明的黑色背景视图。

由于它不是控制器,因此我必须自己管理 UI 旋转(仅适用于 iOS 7,它与 iOS 8 中的 UI 旋转得很好)。