如何创建像Apple一样的Popover ViewController

问题描述 投票:0回答:2

如何在iOS中创建此弹出框ViewController样式?如何使其适合内容并且不超出内容范围?

enter image description here

我尝试将modalPresentation更改为.popover,但就我尝试而言,它仅适用于iPad和macOS,不适用于iPhone。希望有人可以帮助

ios swift iphone xcode uiviewcontroller
2个回答
2
投票

您必须在.popover演示文稿中提出一个新的ViewController。然后,您可以根据需要自定义显示的视图控制器。主View控制器应如下所示:

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
}

0
投票

这对我有用。这可能对您有帮助。

      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
}
}
© www.soinside.com 2019 - 2024. All rights reserved.