Skip to content

Conversation

@Ahamed-Ali
Copy link
Contributor

@Ahamed-Ali Ahamed-Ali commented Apr 16, 2025

Root Cause of the issue

  • When the WebView is placed inside a ScrollView, the height constraint can become infinite. The original code was using constraints directly, which could propagate infinite values. This caused layout issues and potential NaN exceptions.

Description of Change

  • I have properly handled the infinite case for WebView in GetDesiredSize by using size.Width and size.Height instead of relying on potentially infinite constraints.

Regressed PR

Issues Fixed

Fixes #26795

Tested the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Screenshot

Before Issue Fix After Issue Fix
WebViewCrash.mov
WebViewFix.mov

@dotnet-policy-service dotnet-policy-service bot added community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration labels Apr 16, 2025

var width = widthConstraint;
var height = heightConstraint;
var width = size.Width;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this changes, the next conditions like:

if (size.Width == 0)

can use directly the width variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I have modified the changes @jsuarezruiz

@Ahamed-Ali Ahamed-Ali marked this pull request as ready for review April 17, 2025 04:58
@Ahamed-Ali Ahamed-Ali requested a review from a team as a code owner April 17, 2025 04:58
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copilot AI review requested due to automatic review settings December 9, 2025 08:26
@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 29022

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 29022"

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a crash on iOS when a WebView with HeightRequest is placed inside an invisible ScrollView. The fix modifies the GetDesiredSize method in WebViewHandler.iOS.cs to use the computed size dimensions instead of the constraint parameters directly, preventing infinite values from causing layout exceptions.

  • Fixes iOS crash when WebView with HeightRequest is inside invisible ScrollView
  • Updates GetDesiredSize to use size.Width/Height instead of widthConstraint/heightConstraint
  • Adds UI test to prevent regression

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Core/src/Handlers/WebView/WebViewHandler.iOS.cs Modified GetDesiredSize to use size dimensions instead of constraints to prevent infinite value propagation
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26795.cs Added NUnit UI test to verify WebView doesn't crash with HeightRequest in invisible ScrollView
src/Controls/tests/TestCases.HostApp/Issues/Issue26795.cs Added test page reproducing the crash scenario with WebView inside invisible ScrollView
Comments suppressed due to low confidence (1)

src/Core/src/Handlers/WebView/WebViewHandler.iOS.cs:126

  • The logic appears incomplete. When width or height is 0 and the constraint is valid and finite (not infinite, not <= 0), the code should fall back to using the constraint value.

Currently, if size.Width is 0 but widthConstraint is a valid finite value like 300, the code will leave width as 0 instead of using the constraint. This could cause incorrect layout calculations.

Consider adding an else clause to handle the case where constraints are valid:

if (width == 0)
{
    if (widthConstraint <= 0 || double.IsInfinity(widthConstraint))
    {
        width = MinimumSize;
        set = true;
    }
    else
    {
        width = widthConstraint;
        set = true;
    }
}

And similarly for height.

			if (width == 0)
			{
				if (widthConstraint <= 0 || double.IsInfinity(widthConstraint))
				{
					width = MinimumSize;
					set = true;
				}
			}

			if (height == 0)
			{
				if (heightConstraint <= 0 || double.IsInfinity(heightConstraint))
				{
					height = MinimumSize;
					set = true;
				}
			}

Comment on lines +14 to +17
public void WebViewShouldNotCrashWithHeightRequest()
{
App.WaitForElement("Label");
}
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that the app doesn't crash, but doesn't validate the actual layout behavior. Consider adding assertions to verify:

  1. The WebView renders with the correct dimensions when made visible
  2. The HeightRequest is respected
  3. The layout works correctly when the ScrollView's IsVisible property is toggled

Example:

App.Tap("ToggleVisibilityButton");
App.WaitForElement("WebViewId");
var webViewRect = App.FindElement("WebViewId").GetRect();
Assert.That(webViewRect.Height, Is.GreaterThan(0));

This would provide more comprehensive coverage of the layout fix beyond just crash prevention.

Copilot uses AI. Check for mistakes.
Comment on lines +10 to +14
var webView = new WebView
{
Source = "https://en.m.wikipedia.org/wiki",
HeightRequest = 1000,
};
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an AutomationId to the WebView to enable more comprehensive UI testing of the layout behavior:

var webView = new WebView
{
    Source = "https://en.m.wikipedia.org/wiki",
    HeightRequest = 1000,
    AutomationId = "TestWebView"
};

This would allow the test to verify that the WebView is properly sized and positioned, not just that the app doesn't crash.

Copilot uses AI. Check for mistakes.
Comment on lines +22 to +26
var scrollView = new ScrollView
{
IsVisible = false,
Content = webView
};
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a button to toggle the ScrollView's visibility to enable more comprehensive testing:

var toggleButton = new Button
{
    Text = "Toggle Visibility",
    AutomationId = "ToggleVisibilityButton"
};
toggleButton.Clicked += (s, e) => scrollView.IsVisible = !scrollView.IsVisible;
layout.Children.Add(toggleButton);

This would allow the test to verify that the WebView layout works correctly both when initially invisible and when made visible.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-webview WebView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Specifying HeightRequest in Webview when wrapped by ScrollView set "invisible" causes crash in iOS

2 participants