在Swift中从UIButton创建一个popover

时间:2015-03-18 21:21:57

标签: ios swift uibutton popover

我希望从50x50px创建一个关于UIButton的小型popover。我已经看过使用自适应segue的方法,但是我的大小类转向因此意味着我不能使用这个功能!

我怎么能创建这个popover?我可以使用按钮IBACtion中的代码创建它吗?或者我还能用故事板做到这一点吗?

4 个答案:

答案 0 :(得分:12)

您可以执行以下两个选项之一:

  • UIButton中的UIViewController创建一个操作,然后在内部显示您想要的ViewController像Popover,UIViewController必须实现协议{ {1}},请查看以下代码:

    UIPopoverPresentationControllerDelegate

    根据@matt的书 编程iOS 8

      

    iOS 8中的弹出式演示控制器是一个演示控制器(@IBAction func showPopover(sender: AnyObject) { var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardIdentifier") as! UIViewController popoverContent.modalPresentationStyle = .Popover var popover = popoverContent.popoverPresentationController if let popover = popoverContent.popoverPresentationController { let viewForSource = sender as! UIView popover.sourceView = viewForSource // the position of the popover where it's showed popover.sourceRect = viewForSource.bounds // the size you want to display popoverContent.preferredContentSize = CGSizeMake(200,500) popover.delegate = self } self.presentViewController(popoverContent, animated: true, completion: nil) } func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { return .None } ),演示控制器是自适应的。这意味着,默认情况下,在水平紧凑的环境中(即在iPhone上),UIPresentationController模式演示样式将被视为.Popover。 iPad上的popover显示为iPhone上的全屏显示视图,完全取代了界面。

    要避免在iPhone中出现此行为,您需要在.FullScreen中实施委托方法adaptivePresentationStyleForPresentationController以正确显示Popover。

  • 在我看来,另一种方式更容易做,并且正在使用Interface Builder,只需从UIViewController安排创建一个segue到你想要的UIButton和segue选择ViewController segue。

我希望这对你有所帮助。

答案 1 :(得分:7)

Swift 4 这是完全正常运行的代码。所以在这里你会看到大小为250x250的弹出窗口:

    import UIKit

    class ViewController: UIViewController {

        @IBOutlet weak var button: UIButton!

        override func viewDidLoad() {
            super.viewDidLoad()

// in case if you don't want to make it via IBAction
            button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
        }

        @objc
        private func tapped() {

            guard let popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") else { return }

            popVC.modalPresentationStyle = .popover

            let popOverVC = popVC.popoverPresentationController
            popOverVC?.delegate = self
            popOverVC?.sourceView = self.button
            popOverVC?.sourceRect = CGRect(x: self.button.bounds.midX, y: self.button.bounds.minY, width: 0, height: 0)
            popVC.preferredContentSize = CGSize(width: 250, height: 250)

            self.present(popVC, animated: true)
        }
    }


// This is we need to make it looks as a popup window on iPhone
    extension ViewController: UIPopoverPresentationControllerDelegate {

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

请注意,您必须为要作为弹出窗口提供的一个viewController提供popVC标识符。

希望有所帮助!

答案 2 :(得分:2)

在这里,您可以在按钮点击时显示弹出窗口。

func addCategory( _ sender : UIButton) {

        var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
        var nav = UINavigationController(rootViewController: popoverContent)
        nav.modalPresentationStyle = UIModalPresentationStyle.Popover
        var popover = nav.popoverPresentationController
        popoverContent.preferredContentSize = CGSizeMake(50,50)
        popover.delegate = self
        popover.sourceView = sender
        popover.sourceRect = sender.bounds

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

    }

答案 3 :(得分:0)

Swift 4版本

从情节提要中完成大部分工作

我添加了一个ViewController,转到它的属性检查器,并勾选了“使用首选的显式大小”。之后,我将Width和Height值分别更改为50。

完成此操作后,我按ctrl键并将其从“按钮”拖动到ViewController,我选择了“呈现为弹出窗口”,并将segue标识符命名为“弹出”

转到我有按钮的ViewController并添加以下代码:

class FirstViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    @IBOutlet weak var popoverButton: UIButton! // the button

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "pop" {
            let popoverViewController = segue.destination
            popoverViewController.modalPresentationStyle = .popover
            popoverViewController.presentationController?.delegate = self
            popoverViewController.popoverPresentationController?.sourceView = popoverButton
            popoverViewController.popoverPresentationController?.sourceRect  = CGRect(x: 0, y: 0, width: popoverButton.frame.size.width, height: popoverButton.frame.size.height)
        }
    }
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }



    override func viewDidLoad() {
        super.viewDidLoad()
    }

}