0

我很难在 iPhone 的屏幕上实现抽屉滑到一半的演示。

编辑:我发现 iOS 不尊重我在 Segue 中设置的 .custom modalTransitionStyle。如果我在 中明确设置prepareForSegue:,那么它会调用我的委托来获取UIPresentationController.

我有一个自定义 Segue,它也是一个 UIViewControllerTransitioningDelegate。在该perform()方法中,我将目标 transitioningDelegate 设置为 self:

self.destination.transitioningDelegate = self

我要么调用super.perform()(如果它是 Present Modal 或 Present as Popover Segue),或者self.source.present(self.destination, animated: true)(如果它是 Custom Segue,因为调用super.perform()会引发异常)。

perform()andanimationController(…)方法被调用,但从不被调用presentationController(forPresented…)

最初,我尝试使用指定的自定义 Segue 类在 Storyboard “Present Modally”中制作 Segue,但这一直在删除呈现视图控制器。我尝试了“Present as Popover”,我发誓它工作过一次,因为它没有删除呈现视图控制器,但在随后的尝试中它仍然有效。

所以我把它设为“自定义”,并且perform()仍然_UIFullscreenPresentationController在目标视图控制器上通过预设被调用,并且我的presentationController(forPresented…)方法永远不会被调用。

处理此问题的其他解决方案始终取决于该方法的一些错误签名。这是我的,逐字逐句:

public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?

在过去的四天里,我一直在试图找出“正确的”自定义过渡,但事情似乎不像宣传的那样表现得无济于事。我错过了什么?

4

1 回答 1

0

Container View您可以将 a用于您的抽屉,而不是使用自定义演示文稿 segue 。这样,您可以将 UIViewController 用于您的 Drawer 内容,同时避免自定义 segue 的问题。

您可以通过两个步骤实现这一点:首先将 Container View 拉入主视图控制器并正确布局。故事板看起来像这样:(您可以看到您有两个视图控制器。一个用于主视图,一个用于抽屉)

在此处输入图像描述

其次,您创建一个根据需要为抽屉进出动画的动作。一个简单的示例可能如下所示:

@IBAction func toggleDrawer(_ sender: Any) {
    let newHeight: CGFloat
    if drawerHeightConstraint.constant > 0 {
        newHeight = 0
    } else {
        newHeight = 200
    }

    UIView.animate(withDuration: 1) {
        self.drawerHeightConstraint.constant = newHeight
        self.view.layoutIfNeeded()
    }
}

在这里,我只是改变了抽屉的高度约束,将其滑入和滑出。当然你可以做一些更花哨的事情:)

你可以在这里找到一个演示项目。

于 2018-11-14T11:09:11.423 回答