Hi, I'm trying to create a popover implementation in my xamarin forms app. I want to be able to show a xamarin forms' view in an iOS Popover but I'm not able to render my view correctly.
Here's my Xamarin Forms code :
var content = new ContentView
{
BackgroundColor = Color.Coral,
WidthRequest = 300,
HeightRequest = 60,
Content = new Label
{
Text = "Text in Popover",
TextColor = Color.Black,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
BackgroundColor = Color.Yellow
}
};
PopoverManager.ShowPopover(this, content, (Button)sender);
And this is iOS Part :
public void ShowPopover(Page page, VisualElement content, VisualElement origine)
{
var viewController = Platform.GetRenderer(page).ViewController;
var viewOrigine = Platform.GetRenderer(origine).NativeView;
var rect = viewController.View.ConvertRectFromView(viewOrigine.Frame, viewOrigine.Superview);
UIViewController popoverVc = new UIViewController
{
ModalPresentationStyle = UIModalPresentationStyle.Popover
};
popoverVc.View = GetUIView(content);
popoverVc.PopoverPresentationController.SourceView = viewController.View;
popoverVc.PopoverPresentationController.SourceRect = rect;
popoverVc.PreferredContentSize = popoverVc.View.Frame.Size;
popoverVc.PopoverPresentationController.BackgroundColor = popoverVc.View.BackgroundColor;
viewController.PresentViewController(popoverVc, true, null);
}
public static UIView GetUIView(VisualElement view)
{
if (Platform.GetRenderer(view) == null)
Platform.SetRenderer(view, Platform.CreateRenderer(view));
var vRenderer = Platform.GetRenderer(view);
var minSize = vRenderer.Element.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
var rect = new Rectangle(Point.Zero, minSize.Request);
CGRect size = rect.ToRectangleF();
vRenderer.NativeView.Frame = size;
vRenderer.NativeView.TranslatesAutoresizingMaskIntoConstraints = true;
vRenderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
vRenderer.NativeView.ContentMode = UIViewContentMode.ScaleToFill;
vRenderer.Element?.Layout(size.ToRectangle());
var nativeView = vRenderer.NativeView;
nativeView.SetNeedsLayout();
nativeView.SetNeedsDisplay();
return nativeView;
}
Popover presentation works fine and show the contentView with the right size but my Label have a frame equal to 0.
I think I'm missing something to render correctly all children.
My goal is to have more complexe views in popover and I don't want to manually setup size of each children.
Best regards