Swift: how to create a popup menu in iOS

I am drawing a drawing on a custom UIView canvas and instead of having a set of buttons at the bottom of the view for the user to select shapes, I would like the user to make a long press gesture, then a popup menu appears with various shapes that they can select. I don't see anything like this in xCode, although I guess there is something similar in iOS. I don't want the alert popup to appear on low battery and notifications.

I've looked into using UIPopoverController, but I'm a little confused about some of the other questions I've read about this, as well as the documentation provided by Apple.

+4


source to share


3 answers


Once you have connected this button with the viewController and the popover as a segue, you will need to prepare. Here is the following code to prepare for the segue popper.

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if let identifier = segue.identifier
    {
        switch identifier
        {
            case History.SegueIdentifier:

            if let tvc = segue.destinationViewController as? TextViewController
            {
                if let ppc = tvc.popoverPresentationController
                {
                    ppc.delegate = self
                }
                tvc.text = "\(diagnosticHistory)"
            }

            default: break
        }
    }
}

      

Keep in mind that if you have an iPhone, the popover will display in full screen mode, so you can fix this to use text that accepts some specific elements.

This will fix the popover as the size of your element in the text.



@IBOutlet weak var textView: UITextView!
{
    didSet
    {
        textView.text = text
    }
}

var text : String = ""
{
    didSet
    {
        textView?.text = text
    }
}

override var preferredContentSize : CGSize
{
    get
    {
        if textView != nil && presentingViewController != nil
        {
            return textView.sizeThatFits(presentingViewController!.view.bounds.size)
        }
        else
        {
            return super.preferredContentSize
        }
    }

    set {super.preferredContentSize = newValue}

    }
}

      

I have these 2 in different controllers, but I think it will work. You will also need to implement UIPopoverPresentationControllerDelegate

and

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.None
}

      

to your first viewController.

+1


source


Floating Menu

I have described the steps to achieve a floating menu as shown in the image above:

  1. Create segue

    from barButtonItem

    to MenuViewCobtroller

    type 'Present as Popover'
  2. The MenuViewController

    override preferredContentSize

    as:

    override var preferredContentSize : CGSize
    {
        get
        {
            return CGSize(width: 88 , height: 176)
        }
        set 
        {
            super.preferredContentSize = newValue
        }
    }
    
          

In my case, I am returning CGSize

with a width of 100 and a size of 200. You can set these values ​​to match the content of your floating menu. 4. In the source / source view controller in the method, prepare(for segue: sender)

set self

as the delegate popoverPresentationController

:



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowMenuSegue" {
        if let tvc = segue.destination as? MenuViewController
        {
            tvc.delegate = self
            if let ppc = tvc.popoverPresentationController
            {
                ppc.delegate = self
            }
        }
    }
}

      

The original view controller must conform to UIPopoverPresentationControllerDelegate

and implement the following method:

extension ViewController: UIPopoverPresentationControllerDelegate {

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

      

Here it is. You've got a floating menu. Hope this will be helpful.

+1


source


I used the Masture method above and it worked for me (thanks!), But a couple of notes for other newbies like me:

  1. Make sure you list "ShowMenuSegue"

    (or whatever you choose) as the ID of your transition in the storyboard, and

  2. I had to add

    var delegate: MainViewController!
    
          

    in the MenuViewController (with MainViewController being your source view controller) to make it tvc.delegate = self

    work

+1


source







All Articles