Search code examples
xamarinxamarin.iosuipopovercontrolleruipopover

Display UIPopViewController center in iphone and iPad in iOS


I am working on Xamarin.iOS, I want to display UIPopViewController in iphone and iPad in center Screen. The below way I try the code :

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

I tried all the answer in SO and google but nothing is help to me.

Problem 1 : - In Iphone the PopViewController is displaying in the full Screen, but I want to center and small PopView.

Problem 2 : - In Ipad the PopViewController is displaying in the LeftSide I want to display it Center.

Any Help will be Appreciated.


Solution

  • If you don't want this PopoverPresentationController to be shown full screen, you should set a size to its owner Controller: popover.PreferredContentSize = new CGSize(200, 200);. Also you should set the PopoverPresentationController's configuration firstly before you present the popover.

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

    SourceRect means the rectangle in the specified view in which to anchor the popover. You want to show this pop in the center, set this to your View.Frame. Here is my code for you referring to:

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