Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone popup menu like iPad popover?

How can i implement this popup menu in iphone app like a popover in ipad?

alt text


EDIT: This is the best at moment: https://github.com/runway20/PopoverView enter image description here

like image 480
elp Avatar asked Jan 22 '11 16:01

elp


People also ask

How do I show popover in iOS?

Add a new file to the project, Select File -> New ->File and then select iOS -> Source -> Cocoa Touch Class. Name it PopoverViewController and make it a subclass of UIViewController. Go back to Main. storyboard and select the added View Controller.

What is a popover iOS?

A popover is a transient view that appears above other content onscreen when people click or tap a control or interactive area.


2 Answers

iOS 8 and later

Beginning with iOS 8, you can use UIPopoverPresentationController for iPhones in addition to iPads.

enter image description here

Setup

  • Add a UIBarButtonItem to your main View Controller.
  • Add another View Controller to the storyboard. Change it to the size that you want the popover to be and add any content that you want it to have. For my example I just added a UILabel. If you want a whole menu, then just add a table view or list of buttons.
  • Add a segue from the bar button item to the view controller that you will use as the popover. Rather than show, choose Present as Popover.

enter image description here

  • Select the segue in the storyboard and set the identifier to popoverSegue (or whatever string you called it in the code).

enter image description here

  • In the Attributes inspector for the popover view controller, check Use Preferred Explicit Size and confirm that it is the size you want it to be.

enter image description here

Code

This is the code for the main view controller that has the bar button item in it.

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {         if segue.identifier == "popoverSegue" {              let popoverViewController = segue.destinationViewController             popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover             popoverViewController.popoverPresentationController!.delegate = self          }     }      // MARK: - UIPopoverPresentationControllerDelegate method      func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {          // Force popover style         return UIModalPresentationStyle.None     } } 

Popover at an arbitrary anchor point

If you want to set the popover to appear somewhere besides a bar button item (on a UIButton for example) then you need to set the sourceView and sourceRect. See this answer for details.

enter image description here

Further reading

The above example comes mostly from the first link.

  • iPad Style Popovers on the iPhone with Swift
  • iOS 8 Popover Presentations
  • UIPopoverPresentationController on iOS 8 iPhone
  • General overview of popup options in iOS
like image 104
Suragch Avatar answered Oct 28 '22 13:10

Suragch


Have a look at the iPhone UIPopoverController implementation: WEPopover

like image 36
olfuerniss Avatar answered Oct 28 '22 12:10

olfuerniss