【问题标题】:UITabBarController and using transitionFormViewController:UITabBarController 和使用 transitionFormViewController:
【发布时间】:2013-09-09 06:12:29
【问题描述】:

我有一个UITabBarController 作为我的rootViewController,并且基于一个动作,我在tabBarController 上调用presentViewController,它工作正常。呈现的控制器是 UINavigationController

基于我想通过使用transitionFromViewController:toViewController:duration:options:animations:completion: 从当前呈现的 UINavigationController 转换到不同的 UINavigationController 但引发错误的操作:

Parent view controller is using legacy containment in call to -[UIViewController transitionFromViewController:toViewController:duration:options:animations:completion:]

人们谈论在 UINavigationController 上调用它时会得到它,但这是针对 UITabBarController 的,我希望它有所不同。

最终,我想从当前呈现的 UINavigationController 过渡到具有交叉溶解的新 UINavigationController。我可以忽略第一个并展示第二个,但它是从底部滑出的。这可能吗?

【问题讨论】:

    标签: ios uiviewcontroller uinavigationcontroller uitabbarcontroller modalviewcontroller


    【解决方案1】:

    当转换无法按您想要的方式工作时,一种对我始终有效的技巧是简单地使用一些烟雾和镜子。这是一个很好的技术,可以理解并在您的工具带中使用。

    这是我的食谱:

    A)你给你当前的屏幕截图:(借用How Do I Take a Screen Shot of a UIView?

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect rect = [keyWindow bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];   
    UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    B) 使用您的屏幕截图创建一个 UIImageView 并将其添加到您的新 viewController

    UIImageView *fakeScreen = [UIImageView alloc] initWithImage:captureScreen];
     //accessing .view isn't best practice, consider using a dedicated property inside your UIViewController subclass, here just for the sake of explanation
    [secondViewController.view addSubview:fakeScreen];
    

    C) 关闭当前视图控制器,确保不为其设置动画,在您的情况下为 UINavigationController [navigationControllerdismissViewControllerAnimated:NO 完成:nil];

    D) 展示您的新视图控制器,也不要对其进行动画处理。 (secondViewController),然后淡出 imageView。

    [self.tabBarController presentViewController:secondViewController animated:NO completion:NO];
        [UIView animateWithDuration:0.08 animations:^{
            fakeScreen.alpha = 0;
        } completion:
             ^{[
           fakeScreen removeFromSuperview]; ];
    

    这会给你一个正常的淡入淡出。如果你想做一个真正的交叉淡入淡出,你需要对你的第二个viewController的内容有点创意:你需要一个有两个孩子的根视图:一个contentview(用于淡入淡出的内容in) 和 fakeView (将会淡出)。

    【讨论】:

    • 不是一个糟糕的答案,但如果可能的话,我想避免这种情况。
    • 您在问题中描述的错误是什么?
    • 添加到原始问题
    • 鉴于您所拥有的方法,不可能做您想做的事。如果您检查 docs 和 .h 文件,有 2 个危险信号:“最后,接收器不应该是 iOS 容器视图控制器的子类。”以及“此方法仅供自定义容器视图控制器的实现调用”。您在这里没有使用自定义遏制。我的解决方案是一种替代方案,尽管您可能不喜欢,但 Apple 实际上经常使用它来优化复杂的动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2018-08-19
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多