我如何在 iphone 应用程序中实现这个弹出菜单,就像在 ipad 中实现弹出菜单一样?
EDIT: This is the best at moment: https://github.com/runway20/PopoverView
iOS 8 及更高版本
从 iOS 8 开始,您可以使用UIPopoverPresentationController除了 iPad 之外,还适用于 iPhone。
Setup
- Add a
UIBarButtonItem
到你的主视图控制器。
- 将另一个视图控制器添加到故事板。将其更改为您希望弹出窗口的大小,并添加您希望它具有的任何内容。对于我的示例,我刚刚添加了一个
UILabel
。如果您想要整个菜单,只需添加表格视图或按钮列表即可。
- 添加从栏按钮项到将用作弹出框的视图控制器的 Segue。而不是
show
, 选择Present as Popover
.
- 在故事板中选择segue并将标识符设置为
popoverSegue
(或者您在代码中调用它的任何字符串)。
- 在弹出视图控制器的属性检查器中,检查使用首选显式大小并确认它是您想要的尺寸。
Code
这是主视图控制器的代码,其中包含栏按钮项目。
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
}
}
任意锚点处的弹出窗口
如果您想将弹出框设置为显示在栏按钮项目之外的某个位置(在UIButton
例如)那么你需要设置sourceView
and sourceRect
. See 这个答案了解详情。
进一步阅读
上面的例子主要来自第一个链接。
- 使用 Swift 在 iPhone 上实现 iPad 风格的 Popover
- iOS 8 弹出窗口演示
- iOS 8 iPhone 上的 UIPopoverPresentationController
- iOS 中弹出选项的总体概述
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)