在iOS中的iphone和iPad中显示UIPopViewController中心

时间:2018-05-14 05:28:28

标签: xamarin xamarin.ios uipopovercontroller uipopover

我正在研究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显示在LeftSide中,我想将其显示为Center。

任何帮助都会得到赞赏。

1 个答案:

答案 0 :(得分:0)

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

  

在Ipad中,PopViewController显示在我想要的LeftSide中   显示它中心。

SourceRect表示指定视图中用于锚定弹出窗口的矩形。您想在中心显示此弹出窗口,将其设置为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);