首页 > 解决方案 > 如何将 ViewController 呈现为没有背景阴影的模态表

问题描述

有没有什么方法可以将 ViewController 呈现为没有背景阴影的模态表,如下面的第一张图片所示,使用 swift. 有没有简单的方法或者我们需要编写自定义 UIPresentationController?[![所需输出][1]][1]

[1]: https://i.stack.imgur.com/QAEEn.png ![在此处输入图片描述]( https://i.stack.imgur.com/q4JD5.jpg )

标签: swiftxcode

解决方案


您可以根据需要使用较小尺寸的视图控制器。首先添加一个类。

class SmallSizePresentationController : UIPresentationController {
        override var frameOfPresentedViewInContainerView: CGRect {
            get {
                guard let theView = containerView else {
                    return CGRect.zero
                }
                return CGRect(x: 0, y: theView.bounds.height * (281 / 896), width: theView.bounds.width, height: theView.bounds.height)
            }
        }
    }

然后,当您想要呈现这种类型的视图控制器时,只需添加当前视图控制器的扩展。

extension YourCurrentViewController: UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return SmallSizePresentationController(presentedViewController: presented, presenting: presenting)
    }
}

然后像这样展示你的新视图控制器

let VC = withoutShadowVC()
VC.transitioningDelegate = self
VC.modalPresentationStyle = .custom
self.present(VC, animated: true, completion: nil)

您可以修改视图控制器高度。


推荐阅读