OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将 UIViewController 呈现为局部 View ? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:00
标题: ios - 如何将 UIViewController 呈现为局部 View ?

例如,音乐应用程序的“正在播放”屏幕和邮件应用程序的电子邮件撰写屏幕,如下所示:

modal example screenshot modal example screenshot

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



Best Answer-推荐答案


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

首先您需要做的就是关注this UIPresentationController guide原样。

然后将呈现 View Controller 框架调整到大于屏幕的 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
}

然后在调光动画期间设置呈现 View Controller 框架。

    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
    })

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

result!

关于ios - 如何将 UIViewController 呈现为局部 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42567440/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4