我怎样才能用一个按钮做一个弹出视图,然后显示一个 tableView(以弹出格式)?
How can I do a popover view with a button and then show a tableView (in popover format)?
我想在 UIViewController 上创建一个按钮,按下时以弹出窗口格式显示 UITableView。当我尝试时,我必须看到弹出窗口格式的表格视图,但我可以在上面显示我的内容。任何人都知道如何正确地使用 tableView 做一个弹出窗口?或者有人有这种情况的例子吗? (该按钮不是 UIBarButton,它是一个普通的 UIButton)。我试过这个:
import UIKit
class 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
}
}
从您的视图控制器创建一个弹出窗口
通过拖动加号图标在该弹出窗口上添加锚点
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)
}
我想在 UIViewController 上创建一个按钮,按下时以弹出窗口格式显示 UITableView。当我尝试时,我必须看到弹出窗口格式的表格视图,但我可以在上面显示我的内容。任何人都知道如何正确地使用 tableView 做一个弹出窗口?或者有人有这种情况的例子吗? (该按钮不是 UIBarButton,它是一个普通的 UIButton)。我试过这个:
import UIKit
class 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
}
}
从您的视图控制器创建一个弹出窗口
通过拖动加号图标在该弹出窗口上添加锚点
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)
}