Dou*_*ith 12 uiviewcontroller uikit ios ios8 uipresentationcontroller
我一直在关注在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()
}
}
Run Code Online (Sandbox Code Playgroud)
我了解所有这些,除了frameOfPresentedViewInContainerView.它返回一个尺寸,但是,如果我删除presentedView().frame = frameOfPresentedViewInContainerView()中containerViewWillLayoutSubviews这是行不通的.为什么我必须拥有那条线?你会认为函数本身就足够了,否则我只是在containerViewWillLayoutSubviews方法中实现一个随机大小.
的frameOfPresentedViewInContainerView用于通过UIKit中获取初始presentedView的帧,然后将其传递给动画(符合UIViewControllerAnimatedTransitioning协议),以便它建立的动画时知道所呈现视图的目标位置。在呈现动画结束并且呈现在屏幕上之后,或者由于旋转或尺寸类别更改,父视图控制器之一仍可能会调整尺寸。UIPresentationController实例有机会对containerViewWillLayoutSubviews方法中的这些更改做出响应,并适当地调整presentdView的大小。
换句话说,表示控制器始终负责确定所显示视图的布局,但是起初它只是告诉UIKit框架是什么,以便动画制作者可以使用此信息,但是此后,呈现控制器直接在所显示视图上设置框架。