由于您使用的是 iPad,因此您可以通过以编程方式或在故事板中设置视图控制器的首选内容大小来实现。
但我建议你使用presentation controllers。
通过使用它们,您可以返回您喜欢的尺寸或插图。
这是我的一个代码。
class PGSHelpPresentationController: UIPresentationController {
// The dimming view is the view the you have around the the content of the view controller you want to present
let dimmingView = UIView()
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
// here the dimming view is white with an alpha of 0.5
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
}
override public func presentationTransitionWillBegin() {
// Here I'm setting the initial properties of the dimming view (alpha and size)
dimmingView.frame = containerView!.bounds
dimmingView.alpha = 0.0
containerView!.insertSubview(dimmingView, at: 0)
//Here I'm asking to animate the change of alpha along with the transition animation of the view controller
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
context in
self.dimmingView.alpha = 1.0
},
completion: nil)
}
override public func dismissalTransitionWillBegin() { //Here I'm asking that once the dismissal begin I'd like also that the the dimming view refers to sully transparent
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
context in
self.dimmingView.alpha = 0.0
},
completion: {
context in
self.dimmingView.removeFromSuperview()
})
}
//Here I'm setting the size of the view controller that it should be displayed with an inset of 20pt
override public var frameOfPresentedViewInContainerView: CGRect {
return containerView!.bounds.insetBy(dx: 20, dy: 20)
}
override public func containerViewWillLayoutSubviews() {
dimmingView.frame = containerView!.bounds
presentedView!.frame = frameOfPresentedViewInContainerView
}
}
现在我可以创建一个自定义动画,这是一个简单的幻灯片过渡:
class PGSBouncyViewControllerAnimator: NSObject, UIViewControllerAnimatedTransitioning {
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.8
}
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) {
let centre = presentedView.center
presentedView.center = CGPoint(x:centre.x,y: -presentedView.bounds.size.height)
transitionContext.containerView.addSubview(presentedView)
UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: { () -> Void in
presentedView.center = centre
}, completion: { _ in
transitionContext.completeTransition(true)
})
}
}
}
现在我创建转换委托,该对象通过返回演示控制器和自定义动画来管理转换。
public class PGSHelpTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return PGSHelpPresentationController(presentedViewController: presented,
presenting: presenting)
}
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PGSBouncyViewControllerAnimator()
}
}
如果您想展示您的自定义视图控制器,只需在展示的视图控制器中创建转换委托作为属性 (lazy var bouncyTransitioningDelegate = PGSHelpTransitioningDelegate()
) 并执行此操作:
let busyViewController = BusyViewController()
busyViewController.transitioningDelegate = bouncyTransitioningDelegate
busyViewController.modalPresentationStyle = .custom