2017-08-17 61 views
0

我想在UIViewController上創建一個按鈕,並按下時顯示一個UITableView的popover格式。當我嘗試時,我必須看到popover格式的tableview,但我可以在其上呈現我的內容。任何人都知道如何正確使用tableView做一個popover?或者有沒有人有這種情況的例子? (該按鈕不是UIBarButton,它是一個普通的UIButton)。我嘗試這樣做:如何用按鈕做一個彈出視圖,然後顯示一個tableView(以彈出式格式)?

import UIKit 

類StadisticViewController:UIViewController中,UIPopoverPresentationControllerDelegate {

var team: [Players] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 


} 


@IBAction func showPopoverTapped(_ sender: UIButton) { 
    self.performSegue(withIdentifier: "popSegue", sender: self) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "popSegue" { 
     let controller = segue.destination as! PopViewController 
     controller.popTeam = self.team 
     controller.popoverPresentationController!.delegate = self 
     controller.preferredContentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height - 65.0) 

    } 
} 


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

}

+4

請究竟你已經嘗試任何代碼示例。 –

回答

0

創建從您的視圖控制器 一酥料餅通過拖動加號圖標 添加對酥料餅的錨點Popover From a UIView Controller and Button is Anchor

class ViewController : UIViewController , UIPopoverPresentationControllerDelegate { 


    @IBAction func showPopoverTapped(_ sender : UIButton) { 
     self.performSegue("identifier" , sender : self) 
    } 




override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "identifier" { 
     if let controller = segue.destination as? YourViewControllerClass { 
      controller.popoverPresentationController!.delegate = self 
      controller.delegate = self 
      controller.preferredContentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height - 65.0) 
     } 


    } 
} 

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

,如果你想要做編程

 else if senderTag == 2 { 



    let sliderViewController = storyboard?.instantiateViewController(withIdentifier: "BGPopover") as! AllDesignsViewController 
    sliderViewController.modalPresentationStyle = .popover 
    sliderViewController.preferredContentSize = CGSize(width: self.view.frame.width, height: 220) 

    let popoverMenuViewController = sliderViewController.popoverPresentationController 
    popoverMenuViewController?.permittedArrowDirections = .up 
    popoverMenuViewController?.delegate = self 
    // You Still Need Anchor point for popover i used bar button as popover anchor. you can use any other UI Element 
    // popoverMenuViewController?.sourceView = yourAnchorView 
    // popoverMenuViewController?.sourceRect = yourAnchorView.bounds 
    popoverMenuViewController?.barButtonItem = sideBarButton 

    present(sliderViewController, animated: true, completion: nil) 
} 
+0

讓我知道,如果不這樣做;噸適用於亞 –

+0

,因爲有幾種方法來做到這一點 –

+0

我不能嘗試,因爲Xcode說,常量「控制器」需要一個初始化。我不知道如何初始化控制器。謝謝您的幫助。 –

相關問題