问题
Is there something similar to NSPopover for iOS apps? It appears in the Object library for Mac but not for iPhone nor iPad, although I have downloaded apps using this (or at least some very similar) feature.
So my question: Is there a legit way to implement this?
回答1:
UIPopoverController is what you're looking for.
回答2:
There is UIPopOverController, but its use is restricted to iPads:
Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.
(source)
For iPhone/iPod touch, you could use an external framework, like WEPopOver.
回答3:
There is they are called UIPopovers,
Heres a tutorial: http://www.youtube.com/watch?v=1iykxemuxbk
回答4:
For the sake of those Googling this, UIPopoverController is now available for both iPhone and iPad. You can make popovers that look the same on both (as of iOS 9, I believe).
Step 1: Include UIAdaptivePresentationControllerDelegate and UIPopoverPresentationControllerDelegate on your class definition
Step 2: Override the presentation somewhere in your class like this:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
Step 3: When you present a view controller, you tell it to use popover style. Something like this:
//I pull my popover from a separate storyboard, but any modal view will do
let storyboard = UIStoryboard(name: "Popovers", bundle: nil)
let modal = storyboard.instantiateViewController(withIdentifier: "AnyPickerModal") as! AnyPickerVC
modal.modalPresentationStyle = UIModalPresentationStyle.popover
let pc = modal.popoverPresentationController
pc?.permittedArrowDirections = .any
pc?.sourceView = <your button or view you tap to show the popover>
pc?.sourceRect = <your button or view>.bounds
//How big the popover should be
modal.preferredContentSize = CGSize(width: 300, height: 180)
pc?.delegate = self
self.present(modal, animated: true, completion: nil)
Your presented modal will show up as a popover on both iPhone and iPad. Attached is a screenshot from my iPhone app.
Happy coding!
来源:https://stackoverflow.com/questions/6989486/nspopover-for-ios