Xamarin.iOS UIPopoverPresentationController异常 [英] Xamarin.iOS UIPopoverPresentationController Exception

查看:153
本文介绍了Xamarin.iOS UIPopoverPresentationController异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我在网上可以找到的内容,包括UIPopoverPresentationController展示Popover. "nofollow noreferrer>此 this .没有任何帮助.这是我创建的测试应用:

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),
     };
  }

然后我按下按钮调用:

  PresentViewController(_menuController, true, null);

当演示样式为Popover时,出现此异常:

I get this exception when the presentation style is Popover:

UIPopoverPresentationController应该具有一个非null的sourceView或 在演示发生之前设置barButtonItem.

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

推荐答案

每个UIViewController都有一个名为PopoverPresentationControllerUIPopoverPresentationController,您可以使用它来显示弹出视图:

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);
}

我发现当我们关闭popoverview时,它将返回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.

此外,如果您想在iPhone上实现相同的效果,请添加UIPopoverPresentationControllerDelegate,例如:

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;
}

为其设置委托:_menuController.PopoverPresentationController.Delegate = new MyPopOverViewDelegate();

这篇关于Xamarin.iOS UIPopoverPresentationController异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆