The modalPresentationStyle
文档 https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle告诉我们
在水平紧凑的环境中,模态视图控制器始终以全屏方式呈现。
因此,如果您想在 iPhone 的纵向模式下执行此操作,则必须指定.custom
演示风格并让您的过渡代理提供自定义演示控制器。
我个人会让我的第二个视图控制器管理它自己的呈现参数,所以我的第一个视图控制器可能只会:
class FirstViewController: UIViewController {
@IBAction func didTapButton(_ sender: Any) {
let controller = storyboard!.instantiateViewController(withIdentifier: "SecondViewController")
present(controller, animated: true)
}
}
然后我的第二个视图控制器将指定自定义转换并指定自定义转换委托:
class SecondViewController: UIViewController {
private var customTransitioningDelegate = TransitioningDelegate()
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
}
private extension SecondViewController {
func configure() {
modalPresentationStyle = .custom
modalTransitionStyle = .crossDissolve // use whatever transition you want
transitioningDelegate = customTransitioningDelegate
}
}
然后该转换委托将提供自定义呈现控制器:
class TransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return PresentationController(presentedViewController: presented, presenting: presenting)
}
}
该演示控制器将指定其大小:
class PresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
let bounds = presentingViewController.view.bounds
let size = CGSize(width: 200, height: 100)
let origin = CGPoint(x: bounds.midX - size.width / 2, y: bounds.midY - size.height / 2)
return CGRect(origin: origin, size: size)
}
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
presentedView?.autoresizingMask = [
.flexibleTopMargin,
.flexibleBottomMargin,
.flexibleLeftMargin,
.flexibleRightMargin
]
presentedView?.translatesAutoresizingMaskIntoConstraints = true
}
}
这只是自定义过渡的冰山一角。您可以指定动画控制器(用于自定义动画)、调暗/模糊背景等。请参阅 WWDC 2013使用视图控制器自定义转换 https://developer.apple.com/videos/play/wwdc2013/218/有关自定义过渡的入门视频和 WWDC 2014 视频查看 iOS 8 中的控制器改进 https://developer.apple.com/videos/play/wwdc2014/214/ and 演示控制器内部一览 https://developer.apple.com/videos/play/wwdc2014/228/深入研究呈现控制器的细节。
例如,当您呈现模态视图时,您可能希望使背景变暗和模糊。所以你可以添加presentationTransitionWillBegin
and dismissalTransitionWillBegin
要以动画方式呈现此“变暗”视图:
class PresentationController: UIPresentationController {
...
let dimmingView: UIView = {
let dimmingView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
dimmingView.translatesAutoresizingMaskIntoConstraints = false
return dimmingView
}()
override func presentationTransitionWillBegin() {
super.presentationTransitionWillBegin()
let superview = presentingViewController.view!
superview.addSubview(dimmingView)
NSLayoutConstraint.activate([
dimmingView.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
dimmingView.trailingAnchor.constraint(equalTo: superview.trailingAnchor),
dimmingView.bottomAnchor.constraint(equalTo: superview.bottomAnchor),
dimmingView.topAnchor.constraint(equalTo: superview.topAnchor)
])
dimmingView.alpha = 0
presentingViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
self.dimmingView.alpha = 1
}, completion: nil)
}
override func dismissalTransitionWillBegin() {
super.dismissalTransitionWillBegin()
presentingViewController.transitionCoordinator?.animate(alongsideTransition: { _ in
self.dimmingView.alpha = 0
}, completion: { _ in
self.dimmingView.removeFromSuperview()
})
}
}
得出: