How can I change background color of items of a ListView's selected item in the latest version of Xamarin.Forms for iOS platform?
For old iOS platform, the selected item's background color can be changed by writing a custom ViewCellRenderer:
public class ViewCellRenderer : Xamarin.Forms.Platform.iOS.ViewCellRenderer
{
public ViewCellRenderer()
{
}
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
UITableViewCell cell = base.GetCell(item, reusableCell, tv);
ListView listView = cell?.Parent as ListView;
if (listView != null && listView.SelectionMode == ListViewSelectionMode.None)
{
uiCell.SelectionStyle = UITableViewCellSelectionStyle.None;
}
else
{
uiCell.SelectedBackgroundView = new UIView
{
BackgroundColor = ColorCache.ListViewSelectedItemBackgroundColor.ToUIColor()
};
}
return cell;
}
}
But for the latest iOS 15/16, this method does not work again.
After fetching the view structure from Xcode by run the app to the simulator, I found that the new iOS applys the gray background color like this:
>UITableView->Xamarin_Forms_Platform_iOS_ContextActionCell->UITableViewCellSelectedBackgroundThen you can add a new ListViewRender to override the default selectedBackgroundView's backgroundColor value: