-1

我试图将一个显示UIViewController为另一个弹出窗口。为此,我建立了以下...

func showPopover(ofViewController popoverViewController: UIViewController, sender: UIView) {
    popoverViewController.modalPresentationStyle = .popover
    popoverViewController.popoverPresentationController?.sourceView = sender
    popoverViewController.popoverPresentationController?.sourceRect = sender.bounds
    popoverViewController.popoverPresentationController?.delegate = self
    self.present(popoverViewController, animated: true, completion: nil)
}

但是,新的 VC 在紧凑型设备上始终显示为全屏模式演示,而不是实际的弹出框。根据我在这里这里阅读的内容,这是正常行为,但应该可以通过委托进行定制。

我已将呈现的 VC 声明为 implementation UIPopoverPresentationControllerDelegate,将其设置为委托,并实现所需的方法;但是,委托方法永远不会被调用。这意味着无论如何,“弹出框”仍然以模态显示。

任何的建议都受欢迎。

其他一些标注:

  • 如果在它之前添加了一个标记,viewControllerForAdaptivePresentationStyle 会被调用@objc,但这对其他人不起作用。
  • Xcode 对每一个都给出警告:Instance method ... 几乎符合协议 'UIAdaptivePresentationControllerDelegate' 的可选要求... 但是,方法签名是 100% 匹配的。不确定这是否是这个 bug的一个实例,有人说它仍然存在于 Xcode 10.1 中。

谢谢。

实现的委托功能:

func adaptivePresentationStyle(for: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.popover
}

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.popover
}

func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
    switch style {
    case .fullScreen: // Configuration for full-screen
    default: return controller.presentedViewController
    }
}
4

1 回答 1

1

感谢Paulw11确认问题是UIViewController. 这导致了奇怪的行为,因此代码没有像往常一样被调用。

当迁移到 的共享子类UIViewController,以下问题全部解决:

  • adaptivePresentationStyle方法永远不会被调用。
  • 只有在标签viewControllerForAdaptivePresentationStyle前面时才调用该方法。@objc
  • Xcode 提供实例方法...几乎符合协议“UIAdaptivePresentationControllerDelegate”错误的可选要求。

更正的代码如下,适用于任何寻求相同功能的人。

class CustomViewController: UIViewController {

    func showPopover(ofViewController popoverViewController: UIViewController, originView: UIView) {
        popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
        if let popoverController = popoverViewController.popoverPresentationController {
            popoverController.delegate = self
            popoverController.sourceView = originView
            popoverController.sourceRect = originView.bounds
            popoverController.backgroundColor = popoverViewController.view.backgroundColor
            popoverController.permittedArrowDirections = UIPopoverArrowDirection.any
        }
        self.present(popoverViewController, animated: true)
    }
}

extension CustomViewController: UIPopoverPresentationControllerDelegate {

    func adaptivePresentationStyle(for: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
        //return UIModalPresentationStyle.fullScreen
    }

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        if traitCollection.horizontalSizeClass == .compact {
            return UIModalPresentationStyle.none
            //return UIModalPresentationStyle.fullScreen
        }
        //return UIModalPresentationStyle.fullScreen
        return UIModalPresentationStyle.none
    }

    func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
        switch style {
        case .fullScreen: // Configuration for full-screen
        default:
            return controller.presentedViewController
        }
    }
}
于 2019-03-21T02:24:23.127 回答