2016-09-22 58 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

效果不錯,謝謝! –