Xamarin.Forms.iOS中ListView.HasUnevenRows在iPhone12(14.5)失效问题问询
Xamarin.Forms ListView 在iOS14.5 iPhone12自适应行高失效解决方案
这是Xamarin.Forms针对iOS14.5及以上版本的已知兼容问题,不属于配置遗漏,可通过以下方案解决:
方案1:添加iOS端自定义渲染器强制重算行高
针对iOS端单独写ListView的自定义渲染器,强制开启UITableView的自动尺寸计算逻辑,代码示例如下:
using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(ListView), typeof(CustomListViewRenderer))] namespace YourProjectNamespace.iOS.Renderers { public class CustomListViewRenderer : ListViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs<ListView> e) { base.OnElementChanged(e); if (Control == null || Element == null || !Element.HasUnevenRows) return; Control.EstimatedRowHeight = UITableView.AutomaticDimension; Control.RowHeight = UITableView.AutomaticDimension; Control.ReloadData(); } } }
替换代码中的YourProjectNamespace为你自己项目的命名空间即可。
方案2:替换为CollectionView
如果可以调整布局控件,直接将ListView替换为CollectionView即可:
- 设置
CollectionView.ItemsLayout为VerticalList - 无需额外配置自适应行高属性,iOS14.5+的适配逻辑原生支持,不会出现失效问题
临时修复方案
如果不想改动底层代码,可以在ItemsSource数据更新完成后,手动调用ListView.ForceUpdateSize()触发控件重绘,强制刷新所有行的高度。
注意事项
ViewCell内部不要嵌套未设置明确约束的多层布局,避免系统无法计算正确行高- 不要在
ViewCell内部嵌套ScrollView,会干扰行高计算逻辑
内容的提问来源于stack exchange,提问作者Divakar
相关产品推荐
相关产品推荐

