Forum Xamarin.Forms

Prevent iOS behavior - changing background colors of controls in a viewcell after selecting an item

MikilllMikilll Member ✭✭✭

On iOS when you click on an item in ListView, the background color of all controls in a ViewCell is overriden to transparent. This behavior was described earlier many times on websites, like these below:

https://stackoverflow.com/questions/25535426/uitableview-selected-row-changes-subview-background-color
https://forums.xamarin.com/discussion/43807/selected-item-backgroundcolor-overwrites-button-backgroundcolor-listview

The naive solution is to write a custom renderer like this:

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

[assembly: ExportRenderer(typeof(ViewCell), typeof(CustomViewCell))]
namespace GENApp.iOS.Renderers
{
    public class CustomViewCell : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            UITableViewCell cell = base.GetCell(item, reusableCell, tv);
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            return cell;
        }
    }
}

This disables ViewCell selection on iOS and solves the problem with the overriding background color of all controls in a ViewCell on iOS. However, this also disables the tapped effect on iOS, so when a user selects a ViewCell, the background color is not changing at all. I want to preserve this smooth animation when tapping on a view cell. How can I do that?

Sign In or Register to comment.