Apple 更改了演示文稿在 iOS8 中的工作方式,他们使用的是presentationControllers,因为presentationControllers 不是KVO 兼容的,我不得不使用containerView,因为它是removedFromSuperview,并且在调用-[UIPresentationController transitionDidFinish:] 时被取消。 iOS8及以上解决方案:
self.presentationContext.presentViewController(self.viewControllerToPresent, animated: true, completion: { _ in
self.viewControllerToPresent.presentationController?.addObserver(self, forKeyPath: "containerView", options: [], context: &self.observingContext)
})
我添加观察者是 completionHandler 因为演示有时会失败,尤其是在已经演示的 viewController 上演示时。
在观察者值中,当 containerView 不再存在时,我必须删除观察:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard &self.observingContext == context else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
return
}
if let presentationController = object as? UIPresentationController where presentationController.containerView == nil {
presentationController.removeObserver(self, forKeyPath: "containerView")
}
}