Skip to content

Prevent the BlazorWebView bounce on iOS #30176

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

Open
wants to merge 3 commits into
base: net10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ protected override WKWebView CreatePlatformView()
WebView = webview
});

// Disable bounce scrolling to make Blazor apps feel more native
if (webview.ScrollView != null)
{
webview.ScrollView.Bounces = false;
webview.ScrollView.AlwaysBounceVertical = false;
webview.ScrollView.AlwaysBounceHorizontal = false;
}

Logger.CreatedWebKitWKWebView();

return webview;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebView.Maui;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.MauiBlazorWebView.DeviceTests.Components;
using Xunit;

namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests.Elements;

public partial class BlazorWebViewTests
{
#if IOS || MACCATALYST
[Fact]
public async Task BlazorWebViewScrollBounceDisabledByDefault()
{
EnsureHandlerCreated(additionalCreationActions: appBuilder =>
{
appBuilder.Services.AddMauiBlazorWebView();
});

var bwv = new BlazorWebViewWithCustomFiles
{
HostPage = "wwwroot/index.html",
CustomFiles = new Dictionary<string, string>
{
{ "index.html", TestStaticFilesContents.DefaultMauiIndexHtmlContent },
},
};
bwv.RootComponents.Add(new RootComponent { ComponentType = typeof(NoOpComponent), Selector = "#app", });

await InvokeOnMainThreadAsync(async () =>
{
var bwvHandler = CreateHandler<BlazorWebViewHandler>(bwv);
var platformWebView = bwvHandler.PlatformView;
await WebViewHelpers.WaitForWebViewReady(platformWebView);

// Verify that bounce scrolling is disabled by default to make apps feel more native
Assert.False(platformWebView.ScrollView.Bounces);
Assert.False(platformWebView.ScrollView.AlwaysBounceVertical);
Assert.False(platformWebView.ScrollView.AlwaysBounceHorizontal);
});
}
#endif
}
Loading