I'm doing some drawing on a custom UIView canvas, and rather than having a set of buttons at the bottom of the view to allow the user to select shapes, I'd like to have the user do a long press gesture, then have a popup-type menu appear with different shapes they can choose. I don't see anything like this in xCode, though I'd assume there's something like that in iOS. I don't want the alert popup that shows up when you have low battery and notifications.
I've looked into using a UIPopoverController but I'm a bit confused about some of the other Stack Overflow questions I've read about it, and also about the documentation given by Apple.
I described the steps to achieve floating menu as shown in above image:
segue
from the barButtonItem
to the MenuViewCobtroller
of type 'Present as Popover'In the MenuViewController
override the 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 width 100 and size 200. You can set these values so as to fit your floating menu content properly.
4. In the initial/source view controller, in the prepare(for segue: sender)
method set self
as popoverPresentationController
delegate:
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 source view controller must comply to UIPopoverPresentationControllerDelegate
and implement following method:
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
}
That's it. You got the floating menu. Hopefully this will be useful.