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

如何在Xamarin.Forms的iOS 15/16中修改ListView选中项背景色?

如何在最新版Xamarin.Forms iOS平台修改ListView选中项背景色

旧版iOS中通过自定义ViewCellRenderer设置SelectedBackgroundView的方法,在iOS 15/16及以上版本不再生效——这是因为苹果调整了UITableViewCell的选中样式渲染逻辑,默认列表样式会覆盖自定义背景视图设置。以下是适配新版本的两种解决方案:

方案一:优化自定义ViewCellRenderer

通过增强SelectedBackgroundView的配置,并适配iOS 15+系统特性,确保自定义背景生效:

using Xamarin.Forms.Platform.iOS;
using UIKit;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCellRenderer))]
namespace YourAppName.iOS.Renderers
{
    public class CustomViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell(item, reusableCell, tv);
            
            if (cell?.Parent is ListView listView && listView.SelectionMode == ListViewSelectionMode.None)
            {
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            }
            else
            {
                cell.SelectionStyle = UITableViewCellSelectionStyle.Default;
                
                // 创建选中背景视图,设置自适应尺寸确保覆盖整个单元格
                var selectedBgView = new UIView
                {
                    BackgroundColor = Color.FromHex("#E0E0E0").ToUIColor(), // 替换为目标颜色
                    AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
                };
                
                cell.SelectedBackgroundView = selectedBgView;
                
                // iOS 15+ 额外设置未选中时的背景色,避免系统样式干扰
                if (UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
                {
                    cell.BackgroundColor = UIColor.White;
                }
            }
            
            return cell;
        }
    }
}

方案二:重写UITableViewCell的选中状态逻辑

通过自定义原生UITableViewCell,直接控制选中/未选中时的背景色,适配性更灵活:

using Xamarin.Forms.Platform.iOS;
using UIKit;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCellRenderer))]
namespace YourAppName.iOS.Renderers
{
    public class CustomViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            CustomTableViewCell cell = reusableCell as CustomTableViewCell ?? new CustomTableViewCell();
            
            // 绑定Xamarin.Forms Cell内容到原生单元格
            base.SetupCell(cell, item, tv);
            
            if (cell?.Parent is ListView listView && listView.SelectionMode == ListViewSelectionMode.None)
            {
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            }
            else
            {
                cell.SelectionStyle = UITableViewCellSelectionStyle.Default;
                cell.SelectedBgColor = Color.FromHex("#E0E0E0").ToUIColor(); // 设置选中颜色
            }
            
            return cell;
        }
    }
    
    public class CustomTableViewCell : UITableViewCell
    {
        public UIColor SelectedBgColor { get; set; } = UIColor.LightGray;
        
        public override void SetSelected(bool selected, bool animated)
        {
            base.SetSelected(selected, animated);
            // 根据选中状态切换背景色
            BackgroundColor = selected ? SelectedBgColor : UIColor.White;
        }
    }
}

注意事项

  • 替换代码中的YourAppName为你的项目实际命名空间
  • 若使用自定义ViewCell,需将typeof(ViewCell)替换为你的自定义ViewCell类型
  • 可根据需求修改颜色值(支持Color.FromHex、Color.FromRgb等多种方式)

内容的提问来源于stack exchange,提问作者Mark.Fang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 05:43:17