Skip to content

Commit de60821

Browse files
committed
Prevent the BlazorWebView bounce on iOS
1 parent 3f26a59 commit de60821

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ protected override WKWebView CreatePlatformView()
112112
WebView = webview
113113
});
114114

115+
// Disable bounce scrolling to make Blazor apps feel more native
116+
if (webview.ScrollView != null)
117+
{
118+
webview.ScrollView.Bounces = false;
119+
webview.ScrollView.AlwaysBounceVertical = false;
120+
webview.ScrollView.AlwaysBounceHorizontal = false;
121+
}
122+
115123
Logger.CreatedWebKitWKWebView();
116124

117125
return webview;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Components.WebView.Maui;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.Maui.MauiBlazorWebView.DeviceTests.Components;
8+
using Xunit;
9+
10+
namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests.Elements;
11+
12+
public partial class BlazorWebViewTests
13+
{
14+
#if IOS || MACCATALYST
15+
[Fact]
16+
public async Task BlazorWebViewScrollBounceDisabledByDefault()
17+
{
18+
EnsureHandlerCreated(additionalCreationActions: appBuilder =>
19+
{
20+
appBuilder.Services.AddMauiBlazorWebView();
21+
});
22+
23+
var bwv = new BlazorWebViewWithCustomFiles
24+
{
25+
HostPage = "wwwroot/index.html",
26+
CustomFiles = new Dictionary<string, string>
27+
{
28+
{ "index.html", TestStaticFilesContents.DefaultMauiIndexHtmlContent },
29+
},
30+
};
31+
bwv.RootComponents.Add(new RootComponent { ComponentType = typeof(NoOpComponent), Selector = "#app", });
32+
33+
await InvokeOnMainThreadAsync(async () =>
34+
{
35+
var bwvHandler = CreateHandler<BlazorWebViewHandler>(bwv);
36+
var platformWebView = bwvHandler.PlatformView;
37+
await WebViewHelpers.WaitForWebViewReady(platformWebView);
38+
39+
// Verify that bounce scrolling is disabled by default to make apps feel more native
40+
Assert.False(platformWebView.ScrollView.Bounces);
41+
Assert.False(platformWebView.ScrollView.AlwaysBounceVertical);
42+
Assert.False(platformWebView.ScrollView.AlwaysBounceHorizontal);
43+
});
44+
}
45+
#endif
46+
}

0 commit comments

Comments
 (0)