Ios 在Swift中显示来自任意锚点的popover
我知道如何使用工具栏按钮项显示popover,如中所述(适用于iPhone和iPad) 我想为任意锚点添加一个popover。我看到的其他答案是关于条形按钮项目或Objective-C 我刚刚学会了如何做到这一点,所以我在下面添加了我自己的答案。为Swift 3更新 在情节提要中,添加一个视图控制器,使其成为popover。将情节提要ID设置为“popoverId” 还要向主视图控制器添加一个按钮,并将iAction连接到以下代码Ios 在Swift中显示来自任意锚点的popover,ios,swift,popover,Ios,Swift,Popover,我知道如何使用工具栏按钮项显示popover,如中所述(适用于iPhone和iPad) 我想为任意锚点添加一个popover。我看到的其他答案是关于条形按钮项目或Objective-C 我刚刚学会了如何做到这一点,所以我在下面添加了我自己的答案。为Swift 3更新 在情节提要中,添加一个视图控制器,使其成为popover。将情节提要ID设置为“popoverId” 还要向主视图控制器添加一个按钮,并将iAction连接到以下代码 import UIKit class ViewControl
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
@IBAction func buttonTap(sender: UIButton) {
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverId")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender // button
popController.popoverPresentationController?.sourceRect = sender.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}
// UIPopoverPresentationControllerDelegate method
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.none
}
}
通过设置sourceView
和sourceRect
可以选择任意点来显示popover
就这样。现在,当点击按钮时,它应该是这样的
感谢您的帮助。Swift 3.1解决方案: 将以下内容添加到ViewController UIPopoverPresentationControllerDelegate委派:
class OriginalViewController: UIViewController, UIPopoverPresentationControllerDelegate
在ViewController中添加一个按钮,然后点击按钮,调用以下代码:
let controller = MyPopViewController()
controller.modalPresentationStyle = UIModalPresentationStyle.popover
let popController = controller.popoverPresentationController
popController?.permittedArrowDirections = .any
popController?.delegate = self
popController?.sourceRect = (self.myButton?.bounds)!
popController?.sourceView = self.myButton
self.present(controller, animated: true, completion: nil)
更新上述func语法:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentatinStyle {
return .none
}
由于某些原因,旧语法仍然允许使用,但不处于活动状态,无法正确实现弹出或锚定功能。
popController.popopopresentationcontroller?.barbuttoneim=sender
您好,您能告诉我如何制作您所展示的更小的视图控制器吗?@PeterLai,我已经有一段时间没有做过这个了。我不记得我现在做了什么。看看这个问题: