Present a popover from an arbitrary anchor point in Swift
Asked Answered
H

4

11

I know how to present a popover from a bar button item as is described in this answer (for both iPhone and iPad).

enter image description here

I would like to add a popover for an arbitrary anchor point. The other SO answers that I saw were for bar button items or in Objective-C.

I just learned how to do this, so I am adding my own answer below.

Hexastyle answered 5/8, 2016 at 21:13 Comment(0)
H
32

Updated for Swift 3

In the storyboard, add a view controller that you would like to be the popover. Set the Storyboard ID to be "popoverId".

enter image description here

Also add a button to your main view controller and hook up the IBAction to the following code.

import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

    @IBAction func buttonTap(sender: UIButton) {

        // get a reference to the view controller for the popover
        let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverId")

        // set the presentation style
        popController.modalPresentationStyle = UIModalPresentationStyle.popover

        // set up the popover presentation controller
        popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
        popController.popoverPresentationController?.delegate = self
        popController.popoverPresentationController?.sourceView = sender // button
        popController.popoverPresentationController?.sourceRect = sender.bounds

        // present the popover
        self.present(popController, animated: true, completion: nil)
    }

    // UIPopoverPresentationControllerDelegate method
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        // Force popover style
        return UIModalPresentationStyle.none
    }
}

Setting the sourceView and sourceRect is what allows you to choose an arbitrary point to display the popover.

That's it. Now it should like something like this when the button is tapped.

enter image description here

Thanks to this article for help.

Hexastyle answered 5/8, 2016 at 21:13 Comment(3)
popController.popoverPresentationController?.barButtonItem = senderEsdraelon
Hi, can you tell me how to make a smaller view controller like what you showed?Untruth
@PeterLai, It has been a while since I have worked on this. I don't remember what I did right now. Check out this question: #44663502Hexastyle
F
10

Solution for Swift 3.1 :

Add to your ViewController UIPopoverPresentationControllerDelegate delegate :

class OriginalViewController: UIViewController, UIPopoverPresentationControllerDelegate

Add a button to your ViewController and on tap on your button, call this code :

    let controller = MyPopViewController()
    controller.modalPresentationStyle = UIModalPresentationStyle.popover
    let popController = controller.popoverPresentationController
    popController?.permittedArrowDirections = .any
    popController?.delegate = self
    popController?.sourceRect = (self.myButton?.bounds)!
    popController?.sourceView =  self.myButton
    self.present(controller, animated: true, completion: nil)
Figurehead answered 15/5, 2017 at 14:19 Comment(0)
P
3

Update to func syntax above:

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

For some reason the old syntax is still allowed but is not active and will not implement popup or anchor correctly.

Plumbaginaceous answered 15/12, 2019 at 15:11 Comment(0)
I
-1

You can add height and width for the popup:

popController.preferredContentSize = CGSize(width: 200, height: 100)
Inge answered 26/11, 2023 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.