Popover不断呈现模态

时间:2016-09-22 20:56:14

标签: ios swift ios9

我试图在点击图像时添加一个弹出框,但它会一直以模态方式呈现。这个问题/主题的每个答案都建议添加 adaptivePresentationStyleForPresentationController ,但它对我不起作用。我试图在iPhone上这样做。这是我的代码:

class ParkingInfoTableViewController: UITableViewController, UIPopoverPresentationControllerDelegate {
    ...
    func presentPopover(sender:UITapGestureRecognizer) {
    let storyboard : UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
    let infoViewController = storyboard.instantiateViewControllerWithIdentifier("ImagesInfoPopupViewController")
    infoViewController.modalPresentationStyle = .Popover
    infoViewController.preferredContentSize = CGSizeMake(150, 75)

    let popoverPresentationViewController = infoViewController.popoverPresentationController
    popoverPresentationViewController?.permittedArrowDirections = .Any
    popoverPresentationController?.delegate = self
    popoverPresentationViewController?.sourceView = sender.view
    popoverPresentationViewController?.sourceRect = CGRect(
        x: sender.locationInView(sender.view).x,
        y: sender.locationInView(sender.view).y,
        width: 1,
        height: 1)

    self.presentViewController(infoViewController, animated: true, completion: nil)

}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
}

带图像的单元格为:

        let cell = tableView.dequeueReusableCellWithIdentifier(AppConstants.moreInfoCellReusableIdentifier) as! MoreInfoTableViewCell

        let tapped = UITapGestureRecognizer(target: self, action: #selector(presentPopover))
        tapped.numberOfTapsRequired = 1
        cell.securityImage.addGestureRecognizer(tapped)
        cell.securityImage.userInteractionEnabled = true

        return cell

2 个答案:

答案 0 :(得分:3)

以下是Swift 3(Xcode 8)解决方案

从Swift 2.2(Xcode 7)迁移到Swift 3(Xcode 8)时遇到了这个问题。

对于UIPopoverPresentationControllerDelegate我在下面实施了两个:

public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

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

在准备segue时,我在prepare(for segue...)中执行了以下操作:

let popover = segue.destination
popover.popoverPresentationController?.delegate = self
popover.modalPresentationStyle = .popover

假设你有一个popover控制器的类,在viewDidLoad()中你可以复制以下内容:

super.viewDidLoad()
...
self.preferredContentSize = CGSize(width: 123, height: 456)

最后,故事板中定义的我的segue配置如下:

  • 种类:作为Popover出现
  • 锚点:按钮栏项目(因实施而异)
  • 路线:默认情况下全部选中

答案 1 :(得分:0)

另外添加:

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