我一直在关注本教程,了解如何在 iOS 8 中使用 实现自定义视图控制器转换,UIPresentationController
到目前为止这一切都说得通,但我似乎无法让我的视图控制器大小合适。
在该教程中,他们具有以下代码:
class OverlayPresentationController: UIPresentationController {
let dimmingView = UIView()
override init(presentedViewController: UIViewController!, presentingViewController: UIViewController!) {
super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
}
override func presentationTransitionWillBegin() {
dimmingView.frame = containerView.bounds
dimmingView.alpha = 0.0
containerView.insertSubview(dimmingView, atIndex: 0)
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
context in
self.dimmingView.alpha = 1.0
}, completion: nil)
}
override func dismissalTransitionWillBegin() {
presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
context in
self.dimmingView.alpha = 0.0
}, completion: {
context in
self.dimmingView.removeFromSuperview()
})
}
override func frameOfPresentedViewInContainerView() -> CGRect {
return containerView.bounds.rectByInsetting(dx: 30, dy: 30)
}
override func containerViewWillLayoutSubviews() {
dimmingView.frame = containerView.bounds
presentedView().frame = frameOfPresentedViewInContainerView()
}
}
我都明白了,除了frameOfPresentedViewInContainerView
。这会返回一个大小,但是presentedView().frame = frameOfPresentedViewInContainerView()
,如果我删除containerViewWillLayoutSubviews
它就不起作用。为什么我必须有那条线?你会认为函数本身就足够了,否则我只会在containerViewWillLayoutSubviews
方法中实现一个随机大小。