如何在Xamarin iOS(iPhone)中创建popover?

时间:2018-04-06 09:51:29

标签: xamarin.ios

我正在创建一个页面,在单击按钮时,弹出框应该显示为附加的屏幕截图。 我几乎尝试了很多方法。但是最终都显示全屏。是否真的没有办法在Xamarin iOS中创建一个popover?

任何人都可以帮助我吗?enter image description here

1 个答案:

答案 0 :(得分:1)

您可以构建自定义弹出视图控制器,然后使用UIModalPresentationStyle Popover显示它:

//The pop view controller you want to show, here I use Storyboard to initialize it
PopoverViewController popViewController = Storyboard.InstantiateViewController("PopoverViewController") as PopoverViewController;

//Set the presentation style to popover
popViewController.ModalPresentationStyle = UIModalPresentationStyle.Popover;
//The popover view's size
popViewController.PreferredContentSize = new CGSize(150, 150);

//The pop view's position
popViewController.PopoverPresentationController.SourceView = MyBtn;
popViewController.PopoverPresentationController.SourceRect = MyBtn.Bounds;
popViewController.PopoverPresentationController.PermittedArrowDirections = UIPopoverArrowDirection.Up;
//We can also customize the background color
//popViewController.PopoverPresentationController.BackgroundColor = UIColor.White;

popViewController.PopoverPresentationController.Delegate = new PopOverViewDelegate();
PresentModalViewController(popViewController, true);

不要忘记设置下面的代表以在iPhone上实现相同的效果:

public class PopOverViewDelegate : UIPopoverPresentationControllerDelegate
{
    public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
    {
        return UIModalPresentationStyle.None;
    }
}