Little Kid
Little Kid

Reputation: 55

How do we set the default list view selection color for IOS on Xamarin Forms?

I have tried the custom renderer stated here on my project https://blog.falafel.com/adding-transparency-listview-ios-xamarin-forms-custom-renderer/

and https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/ios/theme where I placed the following code below in the finished launching function of app delegate.cs file // switch

UISwitch.Appearance.OnTintColor = UIColor.FromRGB(0x91, 0xCA, 0x47); // green
UITableViewCell.Appearance.TintColor=UIColor.Yellow

But both to no avail. Nothing seems to have changed on the UI and I am not sure if I am missing out on something. Anyone able to help me out with this?

Upvotes: 0

Views: 172

Answers (2)

Ax1le
Ax1le

Reputation: 6643

You can create a custom renderer for ViewCell, then try to change the selected color like:

[assembly: ExportRenderer(typeof(ViewCell), typeof(MyViewCellRenderer))]
namespace ProjectName.iOS
{
    public class MyViewCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell(item, reusableCell, tv);

            cell.SelectedBackgroundView = new UIView(cell.Bounds);
            cell.SelectedBackgroundView.BackgroundColor = UIColor.Red;

            return cell;     
        }
    }
}

Upvotes: 0

Turki Alkhateeb
Turki Alkhateeb

Reputation: 100

I have tried every possible way on the web (I think). And the only way that worked is to make a BoxView with a touch-inside event that fires when user touches the BoxView. Then add that BoxView to your cell's background (presuming that you're using a custom cell for your TableView or ListView). After that you'll have to change the Color property of the BoxView whenever a user touches it.

And since Xamarin forms does not support any touch gesture except for taps. We will need to create our own.

  • How To Create a Custom BoxView

The iOS Renderer (In Xamarin.iOS):

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

[assembly: ExportRenderer(typeof(MyBoxView), typeof(MyBoxViewRenderer))]
namespace something.iOS
{
    public class MyBoxViewRenderer : BoxRenderer
    {
        public MyBoxViewRenderer()
        {
        }

        public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt)
        {
            if (Element == null)
                return;

            var touch = touches.AnyObject as UITouch;
            (Element as MyBoxView).SendTouchEvent(Element as MyBoxView, true);
        }

        public override void TouchesEnded(Foundation.NSSet touches, UIEvent evt)
        {
            if (Element == null)
                return;

            var touch = touches.AnyObject as UITouch;
            (Element as MyBoxView).SendTouchEvent(Element as MyBoxView, false);
        }

        public override void TouchesCancelled(Foundation.NSSet touches, UIEvent evt)
        {
            if (Element == null)
                return;

            var touch = touches.AnyObject as UITouch;
            (Element as MyBoxView).SendTouchEvent(Element as MyBoxView, false);
        }
    }
}

MyBoxView:

using System;
using Xamarin.Forms;

namespace something
{
    public class MyBoxView : BoxView
    {
        public event TouchChanged OnTouchChanged = delegate { };
        public delegate void TouchChanged(object sender, bool IsTouched);

        public void SendTouchEvent(object sender, bool IsTouched)
        {
            OnTouchChanged(sender, IsTouched);
        }

        public MyBoxView()
        {
        }
    }
}

Hope that helps!

Upvotes: 1

Related Questions