在 iphone 中显示 UIPopViewController 中心,在 iOS 中显示 iPad

Display UIPopViewController center in iphone and iPad in iOS

我正在 Xamarin.iOS 上工作,我想在 iphone 和 iPad 中心屏幕中显示 UIPopViewController。我尝试以下代码的方式:

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    showButton.TouchUpInside += (sender, e) => {
    // Create a UIImage view to show in the popover
    UIImageView monkeyIcon = new UIImageView(new CGRect(20, 20, 200, 200));
    monkeyIcon.Image = UIImage.FromFile("Abc.png");
    monkeyIcon.UserInteractionEnabled = true;

    // Create a view controller to act as the popover
    UIViewController popover = new UIViewController();
    popover.View = monkeyIcon;
    popover.ModalPresentationStyle = UIModalPresentationStyle.Popover;

    // Grab Image
    var image = UIImage.FromFile("298-circlex.png");

    // Add a close button
    var closeButton = new UIButton(new CGRect(0, 20, 200, 200));
    closeButton.UserInteractionEnabled = true;
    closeButton.SetTitle("Close", UIControlState.Normal);
    monkeyIcon.AddSubview(closeButton);

    // Wireup the close button
    closeButton.TouchUpInside += (button, e2) => {
         popover.DismissViewController(true, null);
    };

    // Present the popover
    PresentViewController(popover, true, null);

    // Configure the popover for the iPad, the popover displays as a modal view on the
    //iPhone
    UIPopoverPresentationController presentationPopover = popover.PopoverPresentationController;
    if (presentationPopover != null)
    {
        presentationPopover.SourceView = this.View;
        presentationPopover.PermittedArrowDirections = 0;
        presentationPopover.SourceRect = showButton.Frame;
        presentationPopover.Delegate = this;
     }
   };
}

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

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }

我尝试了 SO 和 google 中的所有答案,但对我没有任何帮助。

问题 1: - 在 Iphone 中,PopViewController 全屏显示,但我想居中并缩小 PopView。

问题 2 : - 在 Ipad 中,PopViewController 显示在左侧,我想将其显示在中心。

我们将不胜感激。

如果您不希望此 PopoverPresentationController 全屏显示,您应该为其所有者控制器设置一个大小:popover.PreferredContentSize = new CGSize(200, 200);。此外,您应该先设置 PopoverPresentationController 的配置,然后再显示 popover

In Ipad the PopViewController is displaying in the LeftSide I want to display it Center.

SourceRect 表示指定视图中用于锚定弹出框的矩形。你想在中心显示这个 pop,将它设置为你的 View.Frame。这是我的代码供您参考:

UIPopoverPresentationController presentationPopover = popover.PopoverPresentationController;
if (presentationPopover != null)
{
    presentationPopover.SourceView = this.View;
    presentationPopover.PermittedArrowDirections = 0;
    presentationPopover.SourceRect = View.Frame;
    //Configure this Delegate first before presenting.
    presentationPopover.Delegate = this;
}

PresentViewController(popover, true, null);