You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI Blazor iOS端软键盘唤起页面滚动问题求助

解决.NET MAUI Blazor iOS端键盘唤起后页面整体滚动问题

一、调整iOS原生WebView配置

你之前设置AdjustsForKeyboard = false没生效,是因为没作用到BlazorWebView底层的WKWebView上,需要通过自定义Handler直接修改其滚动行为:

  1. 在项目Platforms/iOS目录下创建自定义处理类:
using Microsoft.Maui.Handlers;
using WebKit;

namespace YourAppNamespace.Platforms.iOS;

public class CustomBlazorWebViewHandler : BlazorWebViewHandler
{
    protected override void ConnectHandler(WKWebView platformView)
    {
        base.ConnectHandler(platformView);
        // 禁用WebView的滚动功能
        platformView.ScrollView.ScrollEnabled = false;
        // 关闭键盘自动调整行为
        platformView.ScrollView.AdjustsForKeyboard = false;
    }
}
  1. 在MauiProgram.cs中注册这个Handler:
builder.ConfigureMauiHandlers(handlers =>
{
#if IOS
    handlers.AddHandler<BlazorWebView, CustomBlazorWebViewHandler>();
#endif
});

二、JavaScript+CSS组合修复(Web页面内部)

如果原生配置仍有遗漏,可通过JS监听焦点事件动态锁定页面滚动:

  1. 在wwwroot/index.html中添加CSS:
/* 禁止页面滚动的样式类 */
.no-scroll {
    overflow: hidden !important;
    position: fixed !important;
    width: 100% !important;
    top: 0 !important;
}
  1. 同样在index.html中添加JS逻辑:
// 输入框获焦时锁定滚动
document.addEventListener('focusin', (e) => {
    const target = e.target;
    if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') {
        document.body.classList.add('no-scroll');
    }
});

// 输入框失焦时恢复滚动
document.addEventListener('focusout', () => {
    document.body.classList.remove('no-scroll');
});

三、Blazor组件级针对性处理(可选)

如果部分输入框需要单独控制,可在组件内通过IJSRuntime调用JS:

  1. 在index.html中定义全局JS函数:
window.lockPageScroll = () => {
    document.body.classList.add('no-scroll');
};

window.unlockPageScroll = () => {
    document.body.classList.remove('no-scroll');
};
  1. 在Blazor组件中调用:
@inject IJSRuntime JS

<input type="text" 
       @onfocus="() => JS.InvokeVoidAsync("lockPageScroll")" 
       @onblur="() => JS.InvokeVoidAsync("unlockPageScroll")" />

补充说明

  • 若禁用全局滚动后部分输入框被键盘遮挡,可单独给这些输入框添加局部滚动逻辑(仅滚动所在容器而非整个页面)。
  • Android端无需额外配置,保持默认行为即可。

内容的提问来源于stack exchange,提问作者iPXvD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:49:56