I have a StackLayout
with a WebView
followed by some other controls. The WebView
should be as high as its contents.
Unfortunately the WebView
doesn't occupy any space at all unless I set its HeightRequest
.
For Android I found this SO question which uses the ViewTreeObserver
. I tried that in a custom renderer and I do get a height at some point in time, but when I tried to set the Layout of the WebView
, then the controls below it will not move down. It I call ForceLayout()
on the StackLayout
, then the method is called recursively (even though I removed the PreDraw handler already).
private void ViewTreeObserver_PreDraw(object sender, ViewTreeObserver.PreDrawEventArgs e)
{
var height = Control.ContentHeight;
if(height > 0 && height != Element.Height && Element.HeightRequest <= 0)
{
Control.ViewTreeObserver.PreDraw -= ViewTreeObserver_PreDraw;
Element.Layout(new Rectangle(Element.X, Element.Y, Element.Width, height));
((StackLayout)Element.Parent).ForceLayout();
}
}
How can I set the Height
of the WebView
to be as high as its content on Android and iOS?