iPhone popup menu like iPad popover?
Solution 1
Have a look at the iPhone UIPopoverController implementation: WEPopover
Solution 2
iOS 8 and later
Beginning with iOS 8, you can use UIPopoverPresentationController
for iPhones in addition to iPads.
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
, choosePresent as Popover
.
- Select the segue in the storyboard and set the identifier to
popoverSegue
(or whatever string you called it in the code).
- 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.
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.
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
Solution 3
On iPhone you would generally use a UIActionSheet for a stack of buttons like that. It slides up from the bottom, rather than popping up next to the button, but that's the standard behavior on iPhone.
Solution 4
There is one that is even better than WEPopover. Developed by a company called 50pixels, it is called FPPopover.
You can download FPPopover at https://github.com/50pixels/FPPopover
Solution 5
You would have to manually instantiate a UIView using a custom background image or drawing with transparency, add some UIButtons (or other type of custom view) on top, and also somehow handle all touches outside that view.
Note that is is non-standard UI. An actionsheet would be more HIG compliant.
Comments
-
elp over 3 years
How can i implement this popup menu in iphone app like a popover in ipad?
EDIT: This is the best at moment: https://github.com/runway20/PopoverView
-
elp over 13 yearsuhm... how can i implement it? PS: the screenschot above come from iphone app!
-
user102008 about 13 years@Paska: which iphone app is this? i'm curious
-
elp about 13 years@user102008 I don't remember, but is a view with rounded corner and an arrow image! Nothing so easy!
-
Dilshan about 12 yearsYa better to stick with action sheet cos most of the iPhone users have used to it.
-
Luke47 about 12 yearsThank you. Do you know if using WEPopover in my app is ok if I want to put it on the Store?
-
clozach about 12 yearsShould be. I don't know the details since a colleague did the implementation, but we've got a modified version of it in our live app.
-
Matt almost 12 yearsThis doesn't work with
UIBarButtonItems
(in my limited experience anyway) -
Herr Grumps over 11 yearsIt is worth noting that the Facebook iOS app uses this "popover" style on the iPhone, even though standard UIKit only allows popovers on the iPad. Given that there are many users of the Facebook app, one can assume that a large number of people will now be quite accustomed to a popover style display on an iPhone
-
jjxtra almost 11 yearsAlso has a lot of memory issues logged
-
Gordonium almost 8 yearsSuragch's answer below is now the standard way of doing this after iOS 8.