Skip to content

Commit 89946dd

Browse files
[iOS] Fix for Transparent Background Color not applied to WebView (#28804)
* fixed webview backgroundcolor issue * Test script changes * test sample changes * Test script changes * Added Mac snapshots
1 parent df738bf commit 89946dd

File tree

10 files changed

+89
-0
lines changed

10 files changed

+89
-0
lines changed
40 KB
Loading
35.2 KB
Loading
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 28714, "[iOS] WebView BackgroundColor is not setting correctly", PlatformAffected.iOS)]
4+
public partial class Issue28714 : ContentPage
5+
{
6+
public Issue28714()
7+
{
8+
BackgroundColor = Colors.YellowGreen;
9+
var verticalStackLayout = new VerticalStackLayout();
10+
verticalStackLayout.Spacing = 20;
11+
12+
var webView = new WebView()
13+
{
14+
HeightRequest = 300,
15+
WidthRequest = 400,
16+
BackgroundColor = Colors.Transparent,
17+
Source = new HtmlWebViewSource
18+
{
19+
Html = @"
20+
<!DOCTYPE html>
21+
<html lang='en'>
22+
<body>
23+
<h1>Welcome to WebView</h1>
24+
<p id='message'></p>
25+
</body>
26+
</html>"
27+
}
28+
29+
};
30+
31+
var button = new Button
32+
{
33+
Text = "Change WebView BackgroundColor",
34+
AutomationId = "button"
35+
};
36+
button.Clicked += (s, e) =>
37+
{
38+
webView.BackgroundColor = Colors.Red;
39+
};
40+
41+
verticalStackLayout.Add(button);
42+
verticalStackLayout.Add(webView);
43+
44+
45+
46+
Content = verticalStackLayout;
47+
}
48+
}
20.8 KB
Loading
18.9 KB
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#if TEST_FAILS_ON_WINDOWS // The transparent background color is not working on Windows, refer to https://github.com/microsoft/microsoft-ui-xaml/issues/6527
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
public class Issue28714 : _IssuesUITest
8+
{
9+
public override string Issue => "[iOS] WebView BackgroundColor is not setting correctly";
10+
11+
public Issue28714(TestDevice device)
12+
: base(device)
13+
{ }
14+
15+
[Test, Order(1)]
16+
[Category(UITestCategories.WebView)]
17+
public void VerifyWebViewBackgroundColor()
18+
{
19+
App.WaitForElement("button");
20+
VerifyScreenshot();
21+
}
22+
23+
[Test, Order(2)]
24+
[Category(UITestCategories.WebView)]
25+
public void VerifyWebViewDynamicBackgroundColor()
26+
{
27+
App.WaitForElement("button");
28+
App.Tap("button");
29+
VerifyScreenshot();
30+
}
31+
}
32+
#endif
32.2 KB
Loading
29.5 KB
Loading

src/Core/src/Handlers/WebView/WebViewHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public partial class WebViewHandler : IWebViewHandler
3030
[nameof(WebView.Settings)] = MapWebViewSettings
3131
#elif __IOS__
3232
[nameof(WKUIDelegate)] = MapWKUIDelegate,
33+
[nameof(IWebView.Background)] = MapBackground,
3334
#endif
3435
};
3536

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ protected override WKWebView CreatePlatformView() =>
2525
public static void MapWKUIDelegate(IWebViewHandler handler, IWebView webView)
2626
{
2727
if (handler is WebViewHandler platformHandler)
28+
{
2829
handler.PlatformView.UIDelegate = platformHandler._delegate ??= new MauiWebViewUIDelegate(handler);
30+
}
31+
}
32+
33+
static void MapBackground(IWebViewHandler handler, IWebView webView)
34+
{
35+
handler.PlatformView.Opaque = webView.Background is null;
36+
handler.PlatformView.UpdateBackground(webView);
2937
}
3038

3139
public static void MapSource(IWebViewHandler handler, IWebView webView)

0 commit comments

Comments
 (0)