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

.NET MAUI iOS平台Entry/Editor被键盘遮挡的解决方案咨询

iOS平台.NET MAUI Entry/Editor键盘遮挡问题解决方案

针对iOS平台上Entry/Editor被键盘遮挡的问题,我们可以通过监听原生键盘事件、动态调整ScrollView内边距并滚动焦点控件到可见区域来解决,适配所有类型的键盘高度(含自动补全、表情键盘)。

具体实现步骤

1. 编写iOS平台原生键盘监听逻辑

在项目的Platforms/iOS目录下创建KeyboardService.cs,用于处理键盘事件:

using Foundation;
using Microsoft.Maui.Controls.Platform;
using UIKit;

namespace YourAppNamespace.Platforms.iOS;

public static class KeyboardService
{
    private static nfloat _keyboardHeight;

    public static void Initialize()
    {
        UIKeyboard.Notifications.ObserveWillShow(OnKeyboardWillShow);
        UIKeyboard.Notifications.ObserveWillHide(OnKeyboardWillHide);
    }

    private static void OnKeyboardWillShow(object sender, UIKeyboardEventArgs args)
    {
        // 从通知参数中获取键盘高度(含工具栏)
        _keyboardHeight = args.FrameEnd.Height;
        AdjustScrollViewPadding();
    }

    private static void OnKeyboardWillHide(object sender, UIKeyboardEventArgs args)
    {
        _keyboardHeight = 0;
        AdjustScrollViewPadding();
    }

    private static void AdjustScrollViewPadding()
    {
        // 获取当前页面的ScrollView(多ScrollView场景需自行调整定位逻辑)
        var currentPage = Application.Current?.MainPage?.Navigation?.NavigationStack.LastOrDefault();
        if (currentPage is ContentPage page && page.Content is ScrollView scrollView)
        {
            // 转换为iOS原生UIScrollView
            if (scrollView.Handler?.NativeView is UIScrollView nativeScrollView)
            {
                // 动态设置底部内边距
                nativeScrollView.ContentInset = new UIEdgeInsets(0, 0, _keyboardHeight, 0);
                nativeScrollView.ScrollIndicatorInsets = nativeScrollView.ContentInset;
            }
        }
    }

    // 滚动焦点控件到可见区域
    public static void ScrollFocusedElementIntoView()
    {
        var focusedElement = Application.Current?.Windows.FirstOrDefault()?.CurrentPage?.FindByName<VisualElement>(FocusManager.GetCurrentFocusedElement()?.Id.ToString());
        if (focusedElement != null && focusedElement.Parent is ScrollView scrollView)
        {
            scrollView.ScrollToAsync(focusedElement, ScrollToPosition.Center, true);
        }
    }
}

2. 注册键盘服务与编辑事件

在Platforms/iOS/AppDelegate.cs的FinishedLaunching方法中初始化服务并绑定编辑事件:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    // 绑定Entry开始编辑事件,触发滚动
    Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CustomEntry", (handler, view) =>
    {
        handler.PlatformView.DidBeginEditing += (s, e) => KeyboardService.ScrollFocusedElementIntoView();
    });

    // 绑定Editor开始编辑事件,触发滚动
    Microsoft.Maui.Handlers.EditorHandler.Mapper.AppendToMapping("CustomEditor", (handler, view) =>
    {
        handler.PlatformView.DidBeginEditing += (s, e) => KeyboardService.ScrollFocusedElementIntoView();
    });

    KeyboardService.Initialize();
    return base.FinishedLaunching(app, options);
}

3. 页面布局保持原结构

你的原始XAML无需修改,确保输入控件放在ScrollView内即可:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EntryIssueMaui.MainPage">
    <ScrollView>
        <Entry VerticalOptions="End" BackgroundColor="LightCoral" />
    </ScrollView>
</ContentPage>

关键说明

  • 通过原生iOS键盘通知获取精确键盘高度,自动适配带工具栏、表情面板的特殊键盘;
  • 监听输入控件的编辑事件,确保焦点控件滚动到可见区域;
  • 多ScrollView场景需在AdjustScrollViewPadding方法中调整目标ScrollView的定位逻辑;
  • 通知监听由系统自动管理生命周期,无需手动注销。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 03:20:17