我试图用dismissViewControllerAnimated(true,完成:{})来关闭UIViewController,但是尝试执行时会得到EXC_BAD_ACCESS。 (也就是说,工作的十分之一)。
我使用了一个自定义的transitionDelegate,当我未设置该设置时,它就可以正常工作。
ListTransitionDelegate返回动画器和presentationController。
PresentationController看起来像这样
init(presentingViewController: UIViewController!, presentedViewController: UIViewController!, controllerStyle: SideControllerStyle, shortest: CGFloat) {
self.controllerStyle = controllerStyle
self.shortest = shortest
super.init(presentingViewController: presentingViewController, presentedViewController: presentedViewController)
self.dimmingView.backgroundColor = UIColor.blackColor()
var tapGesture = UITapGestureRecognizer(target: self, action: Selector("closePresented"))
dimmingView.addGestureRecognizer(tapGesture)
}
override func adaptivePresentationStyle() -> UIModalPresentationStyle
{
return UIModalPresentationStyle.OverFullScreen
}
override func shouldPresentInFullscreen() -> Bool
{
return true
}
override func presentationTransitionWillBegin() {
var containerView = self.containerView
self.dimmingView.frame = self.containerView.bounds
containerView.insertSubview(self.dimmingView, atIndex:0)
presentedViewController.transitionCoordinator().animateAlongsideTransition({context in
self.dimmingView.alpha = 0.5
}, completion:nil)
}
override func presentationTransitionDidEnd(completed: Bool)
{
if !completed {
self.dimmingView.removeFromSuperview()
}
}
override func dismissalTransitionDidEnd(completed: Bool)
{
self.dimmingView.removeFromSuperview()
}
override func dismissalTransitionWillBegin() {
presentedViewController.transitionCoordinator().animateAlongsideTransition({context in
self.dimmingView.alpha = 0
}, completion: nil)
}
func closePresented() {
var presenting = self.presentingViewController
presentedViewController.dismissViewControllerAnimated(true, completion: nil)
}
override func sizeForChildContentContainer(container: UIContentContainer!, withParentContainerSize parentSize: CGSize) -> CGSize {
var size : CGSize
switch self.controllerStyle {
case .Left, .Right:
size = CGSizeMake(self.shortest, parentSize.height)
case .Bottom, .Top:
size = CGSizeMake(parentSize.width, self.shortest)
default:
size = CGSizeMake(parentSize.width, parentSize.height)
}
return size
}
override func frameOfPresentedViewInContainerView() -> CGRect {
var frame : CGRect
var size = sizeForChildContentContainer(nil, withParentContainerSize: containerView.bounds.size)
switch self.controllerStyle {
case .Left:
frame = CGRectMake(containerView.bounds.size.width - size.width, 0, size.width, size.height)
case .Right:
frame = CGRectMake(0, 0, size.width, size.height)
case .Bottom:
frame = CGRectMake(0, containerView.bounds.size.height - size.height, size.width, size.height)
case .Top:
frame = CGRectMake(0, 0, size.width, size.height)
default:
frame = CGRectMake(0, 0, size.width, size.height)
}
return frame
}
最佳答案
尝试在方案设置中启用僵尸对象。 (请记住在完成操作后将其关闭,因为该更改不会显示在源代码控制中,并且您确实不希望发布没有ARC的应用程序!)
如果收到错误消息
-[_UILayoutGuide superview]: message sent to deallocated instance 0x1781ac6a0
然后将以下代码添加到被关闭的视图控制器中:
deinit {
self.view.removeFromSuperview()
}
UILayoutGuide
是 private 的自动布局类。我认为这是一个错误(并以此形式提交),因为为什么被解雇的视图控制器需要自动布局?从其父视图中移除一个类将抢先阻止在自动布局约束被释放后访问它们。