【问题标题】:app crashed due to UICollectionViewController can't be presentingViewController由于 UICollectionViewController 无法呈现ViewController,应用程序崩溃
【发布时间】:2016-09-05 09:28:21
【问题描述】:

当我尝试从 UICollectionViewController 调用自定义 UIPresentationController 时,我的应用程序崩溃了

@IBAction func showSettingsMenu(sender: UIBarButtonItem) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let mvc = storyboard.instantiateViewControllerWithIdentifier(Constants.SettingsViewControllerId) as? SettingsViewController {        
    mvc.modalPresentationStyle = .Custom
    mvc.transitioningDelegate = self
    presentViewController(mvc, animated: true, completion: nil)
}

}

这是我的自定义演示视图控制器:

class SpecialSizePresentationController: UIPresentationController {
    override func frameOfPresentedViewInContainerView() -> CGRect {
        guard let height = containerView?.bounds.height else {
            return CGRectZero
        }

        guard let width = containerView?.bounds.width else {
            return CGRectZero
        }
        let y = height * 0.6
        let frameHeight = height * 0.4

        return CGRectMake(8, y - 8 , width - 8 - 8 , frameHeight)
    }
}

这是我的 UICollectionViewController 中的 UIViewControllerTransitioningDelegate 协议实现:

func presentationControllerForPresentedViewController(presented:   UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {        
     return SpecialSizePresentationController(presentedViewController: presented, presentingViewController: presenting)
}

应用程序在返回位置崩溃并出现错误(由于呈现为零):

fatal error: unexpectedly found nil while unwrapping an Optional value

是否可以从 UICollectionViewController 呈现自定义 Presentation Controller?

如何实现?

【问题讨论】:

    标签: swift presentviewcontroller


    【解决方案1】:

    我解决了这个问题,使用 source 代替 presenting 并且错误消失了

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      相关资源
      最近更新 更多