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

升级iOS 15后Xamarin.Forms的ListView顶部出现异常空白问题

问题原因

你遇到的ListView顶部空白是iOS平台原生控件的默认特性:iOS 15及以上版本系统对UITableView新增了SectionHeaderTopPadding属性,默认值非0,当ListView没有显式设置Header内容时,就会出现这段额外的顶部空隙。

解决方法

方案1:显式设置ListView Header高度为0(推荐优先使用,改动最小)

直接修改MainPage.xaml中的ListView代码,添加Header配置即可,原有其他代码不需要改动:

<ListView ItemsSource="{Binding Users}"
          BackgroundColor="Blue"
          HasUnevenRows="True">
    <!-- 新增Header配置 -->
    <ListView.Header>
        <StackLayout HeightRequest="0" />
    </ListView.Header>
    <!-- 下方原有ItemTemplate内容保持不变 -->
    <ListView.ItemTemplate>
        ...
    </ListView.ItemTemplate>
</ListView>

方案2:全局自定义渲染器统一处理(适合项目多页面存在相同问题的场景)

如果你的项目多个页面的ListView都有这个问题,可以在iOS项目中添加自定义渲染器,全局关闭这个默认padding:

  1. 在iOS项目下新建类文件CustomListViewRenderer.cs,代码如下:
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
// 替换为你自己项目的命名空间
[assembly: ExportRenderer(typeof(ListView), typeof(YourProjectNamespace.iOS.CustomListViewRenderer))]
namespace YourProjectNamespace.iOS
{
    public class CustomListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);
            // 仅针对iOS15及以上系统生效
            if (Control != null && UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
            {
                Control.SectionHeaderTopPadding = 0;
            }
        }
    }
}

修改完成后重新编译运行即可生效,所有ListView的顶部空白都会被移除。

注:你当前开启的ios:Page.UseSafeArea="true"是适配刘海屏安全区域的配置,和本次ListView的顶部空白无关,不需要修改这个配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 20:24:04