-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iOS WebView in a Layout does not size properly #21604
Comments
I encountered this issue myself while migrating my initial Xamarin.Forms applications to .NET MAUI. While it can be frustrating, it's important to note that this behavior is not a bug but rather by design as the layout logic for StackLayouts got changed between Xamarin.Forms and .NET MAUI. Referencing the documentation, we find the following crucial information:
In addition to setting a specific <Grid BackgroundColor="BlueViolet" RowDefinitions="auto, *">
<Label
x:Name="TitleLabel"
BackgroundColor="Orange"
FontSize="16"
TextColor="Black"
VerticalOptions="Start" />
<WebView
x:Name="WebViewDescription"
Grid.Row="1"
AutomationProperties.IsInAccessibleTree="False"
BackgroundColor="Orange"
HorizontalOptions="Fill" />
</Grid> However, it's important to note that while this solution addresses the layout issue, the WebView will now take all the available space on the screen and may grow larger than the content, resulting in a layout that differs from your Android screenshot. |
Yes, it is very frustrating that Stacked Layouts were changed so they are practically unusable and grids don't fill the gap. And the documentation is not accurate, the WebView IS rendered in both iOS and Android without either HeightRequest or WidthRequest being defined. The real issue is the WebView isn't being resized once its content is loaded on iOS. I was able to get around this using a custom handler, here it is in case anyone else can use it:
|
I have the same issue. The problem with our situation is that we can't set a Height because the data is dynamic and can change. So setting a HeightReqeust to 50 or whatever will not work. @czuck I tried your example but for some reason the DidFinishNavigation is never firing. This used to fire in Xamarin but not in Maui. UPDATE Once I took that out the webView loaded. I then still had the issue of resizing. When the code executed this line: The height was 0. If I added a 5 second delay just before this line it works. The reason why is because once the page is loaded on the screen and showing, it then would show the contentsize correctly. If you try and get the size before the actual page is showing on the device then it just says 0. |
A decent workaround is to get the size of the whole page even if is not shown completely via javascript. You can add to your bottom of your web page the following code: <div id=""page-height""></div>
<script>
const body = document.body;
const html = document.documentElement;
const height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
document.getElementById('page-height').innerHTML = height;
</script> and after the page loads you can call the following code to get the calculated height from the page:
<WebView x:Name="yourwebview">
<WebView.Source>
<HtmlWebViewSource Html="{Binding exampleHtml}" />
</WebView.Source>
</WebView>
var result = await yourwebview.EvaluateJavaScriptAsync($"document.getElementById('page-height').innerText");
Console.WriteLine(result);
yourwebview.HeightRequest = Convert.ToDouble(result.ToString()); I hope that helps. |
Description
On Android the WebView is large enough to display the content:
on iOS it is not:
Steps to Reproduce
Create a file > new Maui app. Change the MainPage.xaml to
and the MainPage.xaml.cs to:
run the app. It was found in version 8.0.14
Link to public reproduction project repository
No response
Version with bug
8.0.10 SR3
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
iOS
Affected platform versions
No response
Did you find any workaround?
No response
Relevant log output
No response
The text was updated successfully, but these errors were encountered: