2016-09-22 60 views
1

我试图添加一个弹出窗口,当一个图像被点击,但它不断呈现模态。此问题/主题的每个答案建议添加adaptivePresentationStyleForPresentationController但它不适用于我。我正试图在iPhone上做到这一点。这是我的代码:Popover不断呈现模态

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 

回答

0

另外补充:

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

还是不行... –

3

以下为夫特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 

假设你有一个类你的酥料饼的控制器,在viewDidLoad()您可以复制以下内容:

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

最后,我是在故事板定义SEGUE配置为如下:

  • 类:存在酥料饼
  • 主持人:一个按钮栏项目(会为你的执行会发生变化)
  • 路线:全部默认检查
+1

效果不错,谢谢! –