如何创建像苹果一样的 Popover ViewController
How to create a Popover ViewController like Apples one
如何在 iOS 中创建此弹出窗口 ViewController
样式?我怎样才能让它适合它的内容并且不超过内容框架?
我试图将 modalPresentation
更改为 .popover
,但据我所知,它仅适用于 iPad 和 macOS,而不适用于 iPhone。希望有人能帮忙
您必须在 .popover 演示中呈现一个新的 ViewController。
然后您可以根据需要自定义呈现的视图控制器。
主视图控制器应如下所示:
class ViewController: UIViewController {
@IBAction func buttonClicked(_ sender: Any) {
//get the button frame
/* 1 */
let button = sender as? UIButton
let buttonFrame = button?.frame ?? CGRect.zero
/* 2 */
//Configure the presentation controller
let popoverContentController = self.storyboard?.instantiateViewController(withIdentifier: "PopoverContentController") as? PopoverContentController
popoverContentController?.modalPresentationStyle = .popover
/* 3 */
// Present popover
if let popoverPresentationController = popoverContentController?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = .up
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = buttonFrame
popoverPresentationController.delegate = self
if let popoverController = popoverContentController {
present(popoverController, animated: true, completion: nil)
}
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
}
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
return true
}
}
PopoverContentController,您将在其中添加您的 TableView,例如
class PopoverContentController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// Custom design&implementation
}
这对我有用。这可能对你有帮助。
var dropDownView : DropDownView!
设置下拉视图
dropDownView = Storyboard.Main.instantiateViewController(withIdentifier: "DropDownView") as? DropDownView
self.dropDownView.delegate = self
self.dropDownView?.preferredContentSize = CGSize(width: 200, height: CGFloat((dropDownView.listArr.count) * 35))
let presentationController = AlwaysPresentAsPopover.configurePresentation(forController: self.dropDownView!)
presentationController.sourceView = sender
presentationController.sourceRect = sender.bounds
presentationController.permittedArrowDirections = [.down, .up]
self.present(self.dropDownView!, animated: true)
//MARK:- 当前控制器的委托方法:单击查看更多图标打开下拉视图
class AlwaysPresentAsPopover : NSObject, UIPopoverPresentationControllerDelegate {
private static let sharedInstance = AlwaysPresentAsPopover()
private override init() {
super.init()
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
static func configurePresentation(forController controller : UIViewController) -> UIPopoverPresentationController {
controller.modalPresentationStyle = .popover
let presentationController = controller.presentationController as! UIPopoverPresentationController
presentationController.delegate = AlwaysPresentAsPopover.sharedInstance
return presentationController
}
}
如何在 iOS 中创建此弹出窗口 ViewController
样式?我怎样才能让它适合它的内容并且不超过内容框架?
我试图将 modalPresentation
更改为 .popover
,但据我所知,它仅适用于 iPad 和 macOS,而不适用于 iPhone。希望有人能帮忙
您必须在 .popover 演示中呈现一个新的 ViewController。
然后您可以根据需要自定义呈现的视图控制器。
主视图控制器应如下所示:
class ViewController: UIViewController {
@IBAction func buttonClicked(_ sender: Any) {
//get the button frame
/* 1 */
let button = sender as? UIButton
let buttonFrame = button?.frame ?? CGRect.zero
/* 2 */
//Configure the presentation controller
let popoverContentController = self.storyboard?.instantiateViewController(withIdentifier: "PopoverContentController") as? PopoverContentController
popoverContentController?.modalPresentationStyle = .popover
/* 3 */
// Present popover
if let popoverPresentationController = popoverContentController?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = .up
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = buttonFrame
popoverPresentationController.delegate = self
if let popoverController = popoverContentController {
present(popoverController, animated: true, completion: nil)
}
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
}
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
return true
}
}
PopoverContentController,您将在其中添加您的 TableView,例如
class PopoverContentController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// Custom design&implementation
}
这对我有用。这可能对你有帮助。
var dropDownView : DropDownView!
设置下拉视图
dropDownView = Storyboard.Main.instantiateViewController(withIdentifier: "DropDownView") as? DropDownView
self.dropDownView.delegate = self
self.dropDownView?.preferredContentSize = CGSize(width: 200, height: CGFloat((dropDownView.listArr.count) * 35))
let presentationController = AlwaysPresentAsPopover.configurePresentation(forController: self.dropDownView!)
presentationController.sourceView = sender
presentationController.sourceRect = sender.bounds
presentationController.permittedArrowDirections = [.down, .up]
self.present(self.dropDownView!, animated: true)
//MARK:- 当前控制器的委托方法:单击查看更多图标打开下拉视图
class AlwaysPresentAsPopover : NSObject, UIPopoverPresentationControllerDelegate {
private static let sharedInstance = AlwaysPresentAsPopover()
private override init() {
super.init()
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
static func configurePresentation(forController controller : UIViewController) -> UIPopoverPresentationController {
controller.modalPresentationStyle = .popover
let presentationController = controller.presentationController as! UIPopoverPresentationController
presentationController.delegate = AlwaysPresentAsPopover.sharedInstance
return presentationController
}
}