【问题标题】:Custom presented UIViewController changing to fullscreen自定义呈现的 UIViewController 更改为全屏
【发布时间】:2014-10-03 05:38:43
【问题描述】:

我有一个使用UIModalPresentationCustom 呈现风格呈现的视图控制器。我使用自定义UIViewControllerTransitioningDelegate 将视图控制器呈现为侧边栏(因此它从屏幕边缘滑入并且不占据全屏)。

然而,当我使用UIModalPresentationFullScreen 呈现另一个视图控制器时——然后关闭全屏视图控制器,我的底层自定义呈现控制器突然调整大小以占据全屏。有谁知道为什么会这样?

编辑:这本质上是我的animateTransition 呈现侧边栏的方法——我已经删除了大部分代码以使其可读。基本上,它从 transitionContext 中获取容器,将目标视图控制器的视图添加到容器中并为其设置动画。

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIView *container = transitionContext.containerView;

    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;

    if( toVC.isBeingPresented )
    {
        [container addSubview:toView];

        //... Animate some new frame for toView            

        //Call [transitionContext completeTransition:YES] on animation completion
    }
    else
    {
        //... Animate fromView out

        //On completion remove fromView from superview

        //Call [transitionContext completeTransition:YES] on animation completion
    }
}

编辑 2:做更多的研究,我注意到当模式堆栈中的视图控制器上方被解除时,我的自定义呈现视图控制器的视图的框架正在设置。以下堆栈跟踪导致框架被设置为全屏:

0 -[MyCustomPresentedViewControllerView setFrame:]
1 -[UIView(MPAdditions) setFrameOrigin:]
2 -[UIViewControllerAccessibility(SafeCategory) dismissViewControllerWithTransition:completion:]
3 -[UIViewController dismissViewControllerAnimated:completion:]

【问题讨论】:

  • 也许问题出在您的过渡委托的代码上。贴出相关代码。

标签: ios objective-c uiviewcontroller presentviewcontroller uimodalpresentationstyle


【解决方案1】:

尝试使用:

viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;

它在 iOS 8.1 中帮助了我

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我已经尝试使用符号断点对其进行调试,并且似乎对某种布局管理器进行了一些内部调用。

    虽然我无法“解决”这个问题(在我看来,这就像 SDK 中的一个错误),但我能够想出一个解决这个问题的解决方法。基本上,您必须在两个合适的时间设置呈现视图的正确尺寸。像这样:

    在使用自定义 UIPresentationController 呈现的视图控制器中,添加以下内容:

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.view.frame = [self.presentationController frameOfPresentedViewInContainerView];
        });
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        self.view.frame = [self.presentationController frameOfPresentedViewInContainerView];
    }
    

    如果您想知道:是的,您确实需要在两个地方使用它。因为奇怪的是,当我只在 viewDidAppear 异步块中执行此操作时,一旦动画完成,它就会损坏(调整为全屏)。

    【讨论】:

    • 这适用于 iOS 9.3.5,我已经测试过了。谢谢
    【解决方案3】:

    通过分配改变演示风格:

    viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;

    导致模态呈现控制器占据屏幕的一小部分,与 UIPresentationController 的原始呈现视图控制器相同。

    但是,使用
    viewController.modalPresentationStyle = UIModalPresentationOverFullScreen
    在这种情况下更好,因为他的模态控制器是全屏的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多