Xamarin.iOS UIPopoverPresentationController Exception

一个人想着一个人 提交于 2019-12-11 15:45:50

问题


I am having difficulty presenting a Popover with UIPopoverPresentationController, following what can I find from the internet, including this and this. Nothing has helped. Here's a test app I created:

ViewController.cs

  public override void ViewDidLoad()
  {
     ...

     var popupView = new UIImageView(new CGRect(0, 0, 200, 200))
     {
        Image = UIImage.FromBundle("Menu")
     };

     _menuController = new UIViewController
     {
        ModalPresentationStyle = UIModalPresentationStyle.Popover,
        View = popupView
     };

     _presentationController = new UIPopoverPresentationController(_menuController, this)
     {
        SourceView = View,
        SourceRect = new CGRect(50, 50, 300, 300),
     };
  }

And I invoke on button press with:

  PresentViewController(_menuController, true, null);

I get this exception when the presentation style is Popover:

UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs.


回答1:


Every UIViewController has a UIPopoverPresentationController called PopoverPresentationController, you can just use that to present your pop view:

private void OnMenuSelected(object sender, EventArgs eventArgs)
{
    var popupView = new UIImageView(new CGRect(0, 0, 200, 200))
    {
        Image = UIImage.FromBundle("Menu"),
        UserInteractionEnabled = true
    };

    _menuController = new UIViewController
    {
        ModalPresentationStyle = UIModalPresentationStyle.Popover,
        PreferredContentSize = new CGSize(200, 200),
        View = popupView
    };

    _menuController.PopoverPresentationController.SourceRect = new CGRect(50, 50, 300, 300);
    _menuController.PopoverPresentationController.SourceView = View;


    PresentViewController(_menuController, true, null);
}

I find that when we dismiss the popoverview, it will return to null. So here I recommend you to initialize it in your method every time when you show it up.

Moreover if you want to achieve the same effect on iPhone, please add the UIPopoverPresentationControllerDelegate like:

public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
{
    return UIModalPresentationStyle.None;
}

Set the delegate to it: _menuController.PopoverPresentationController.Delegate = new MyPopOverViewDelegate();




回答2:


You need to set the UIPopoverPresentationController.SourceView to a UIView that is outside the view controller to provide a view that becomes the anchor point, i.e. a UIView on the parent view controller

You say present this via a button touch, you could use that button as the source (if that location is appropriate).

_presentationController = new UIPopoverPresentationController(_menuController, this)
{
    SourceView = button,
    SourceRect = new CGRect(50, 50, 300, 300),
};

Example:



来源:https://stackoverflow.com/questions/49930691/xamarin-ios-uipopoverpresentationcontroller-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!