在 Xcode beta 2 和 iOS 10 beta 2 之前,我有一个工作正常的自定义UIPresentationController。我没有更改任何代码,但是现在呈现出来的是标准模态呈现。
Apple的UIPresentationController示例代码中有一条注释:
对于将使用自定义呈现控制器的呈现, 它可能也是transitioningDelegate。 这避免了引入另一个对象或 在源视图控制器中实现。
transitioningDelegate不会强制保留其目标对象。 为了防止在调用-presentViewController:animated:completion之前释放presentationController, NS_VALID_UNTIL_END_OF_SCOPE属性附加到声明。
我已经检查了呈现后的视图控制器上的transitioningDelegate。在呈现之前,它是我的自定义UIPresentationController,但呈现后为空。我猜测是引用被释放了,但我找不到Swift中等效的NS_VALID_UNTIL_END_OF_SCOPE。 编辑:我已经验证,在呈现之前设置了transitioningDelegate,但当要进行呈现时就变成了nil。
我在呈现视图控制器中的代码:
@IBAction func buttonAction(_ sender: UIButton) {
let secondViewController = storyboard!.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
let presentationController = MyPresentationController(presentedViewController: secondViewController, presenting: self)
presentationController.initialFrame = button.frame
secondViewController.transitioningDelegate = presentationController
// Move map
let pixelsToMove: CGFloat = mapView.frame.height / 4
let region = self.mapView.region
self.mapView.setRegion(region, offsetBy: pixelsToMove, animated: true)
// Delegate to NewViewController
secondViewController.mapView = mapView
mapView.delegate = secondViewController
print(secondViewController.transitioningDelegate)
UIView.animate(withDuration: 0.3, animations: {
let tabBar = self.tabBarController!.tabBar
tabBar.frame.origin.y += tabBar.frame.height
self.present(secondViewController, animated: true, completion: nil)
})
}
我在UIPresentationController中的代码:
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
presentedViewController.modalPresentationStyle = .custom
}