Popovers和Xamarin iOS

时间:2016-05-09 14:33:07

标签: xamarin.ios modal-dialog uipopovercontroller uipopover

我在使用Popovers的Xamarin iOS实现时遇到了麻烦。

就我而言,它们可通过新的UIPopoverPresentationController类获得,如Apple文档(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/)中所述

我的代码是这样的:

public partial class PopoverSettings : UIViewController
{
    public PopoverSettings () //: base ("PopoverSettings", null)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        View.Frame = new CoreGraphics.CGRect(0,0,100,100);
        this.PreferredContentSize = new CoreGraphics.CGSize (10, 10);
        View.BackgroundColor = UIColor.Red;
    }
}

我尝试从

创建它
var myPop = new PopoverSettings();
myPop.ModalInPopover = true;
myPop.ModalPresentationStyle = UIModalPresentationStyle.Popover;
var pc = (UIPopoverPresentationController)myPop.PresentationController;
pc.SourceView = navigation;
pc.SourceRect = new CGRect(0,0,100,100);
this.PresentViewController(myPop, true, null);          

结果正常,但是我得到了一个全屏视图控制器,而不是“Popover”组件!

有人知道为什么会这样吗?谢谢!

1 个答案:

答案 0 :(得分:0)

如果您在iPhone上实现此功能,框架会自动将您的PresentationStyle更改为全屏。为了防止这种情况,你必须实现IUIPopoverPresentationControllerDelegate接口回调' adaptivePresentationStyleForPresentationController'。

[Export("adaptivePresentationStyleForPresentationController:")]
public UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
{
    return UIModalPresentationStyle.None;
}

然后,您必须确保将PresentationController的delegate属性设置为您实现回调的类的引用。

pc.delegate = this;

框架现在将调用您的回调,您可以强制它使其成为一个弹出窗口。