如何将 UIViewController 呈现为局部视图?

How to present UIViewController as partial view?

例如,音乐应用的 "now playing" 屏幕和邮件应用的电子邮件撰写屏幕如下所示:

我该如何表达我的观点?我假设它不能仅使用故事板来实现,因为所有选项似乎都覆盖了全屏,而不是所需的部分模态。

I'm assuming it can't be achieve purely on storyboards alone...

这不是最近的问题,所以我把它留待以后使用。希望你已经做到了。

首先你需要做的就是按原样关注this UIPresentationController guide

然后将 presented 视图控制器框架调整到大于屏幕的 2/3。

override func containerViewWillLayoutSubviews() {
    presentedView?.frame = frameOfPresentedViewInContainerView
    presentedView?.layer.cornerRadius = 5.0
    presentedView?.layer.masksToBounds = true
}

override func size(forChildContentContainer container: UIContentContainer,
                   withParentContainerSize parentSize: CGSize) -> CGSize {
    switch direction {
        case .left, .right:
            return CGSize(width: parentSize.width*(2.0/3.0), height: parentSize.height)
        case .bottom, .top:
            return CGSize(width: parentSize.width, height: parentSize.height-40.0)
    }
}

override var frameOfPresentedViewInContainerView: CGRect {
    var frame: CGRect = .zero
    frame.size = size(forChildContentContainer: presentedViewController,
                withParentContainerSize: containerView!.bounds.size)

    switch direction {
        case .right:
            frame.origin.x = containerView!.frame.width*(1.0/3.0)
        case .bottom:
            frame.origin.y = 40.0
        default:
            frame.origin = .zero
    }
    return frame
}

然后在调光动画期间设置呈现视图控制器框架。

    self.presentingFrame = self.presentingViewController.view.frame
    var frame = self.presentingFrame
    frame.size.width -= 40.0
    frame.size.height -= 60.0
    frame.origin.x += 20.0
    frame.origin.y += 30.0

    coordinator.animate(alongsideTransition: { _ in
        self.dimmingView.alpha = 1.0

        self.presentingViewController.view.frame = frame

        self.presentingViewController.view.layer.cornerRadius = 5.0
        self.presentingViewController.view.layer.masksToBounds = true

        UIApplication.shared.statusBarStyle = .lightContent
    })

别忘了添加反向动画。结果: