Reputation: 129
I am unable to change the background color on the selected item in a listview from gray on iOS. I am using Xamarin.Forms 2.3.4.192-pre2, before I used 2.3.3 with the same result.
I have tried to impalement a custom renderer:
using App.Views.View.List;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(NoSelectionViewCell), typeof(App.iOS.Views.List.NoSelectionViewCellRenderer))]
namespace App.iOS.Views.List
{
public class NoSelectionViewCellRenderer: ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
UITableViewCell cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None; // This does nothing ...
// cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; // This does nothing ...
// cell.SelectionStyle = UITableViewCellSelectionStyle.Default; // This does nothing ...
// cell.SelectionStyle = UITableViewCellSelectionStyle.Gray; // This does nothing ...
if (cell != null)
{
cell.SelectedBackgroundView = new UIView
{
BackgroundColor = UIColor.Red, // This doesn't matter ...
};
}
UpdateBackground(cell, item); // This doesn't seem to do anything ...
return cell;
}
}
}
I want the background to be transparent.
Upvotes: 1
Views: 1943
Reputation: 33
Not sure if Apple/Xamarin fixed anything but the no selection style is definitely working for me, exert included, but see working sample project:
using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using ListviewNoSelection.Controls;
[assembly: ExportRenderer(typeof(NoSelectionCell), typeof(ListviewNoSelection.iOS.Renderer.NoSelectionCellRenderer))]
namespace ListviewNoSelection.iOS.Renderer
{
public class NoSelectionCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
UITableViewCell cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
https://github.com/conrad90909/NoSelectionViewCell
Upvotes: 1
Reputation: 9521
I tried your code and it works fine.
I just changed the export renderer like this :
[assembly: ExportRenderer(typeof(ViewCell), typeof(NoSelectionViewCellRenderer))]
I verified that the breakpoint is hit and it work fine. I commented this line
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
And the selected background is red. Your code is fine!
Upvotes: 0
Reputation: 9521
What I do in my projects is to deselect the item once it is selected. It solves the problem that you cannot select an item already selected (for example, you select an item, you navigate to new page then you navigate back and the item is already selected. In this case, you cannot reselect the same item).
This is how I do it:
private void ListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
//we do stuff here
// We deselect the item so that the background is not greyed when we come back
ListView.SelectedItem = null;
}
This is the xaml
<ListView x:Name="ListView"
ItemSelected="ListView_OnItemSelected">
</ListView>
Upvotes: 1