dotMorten / MauiEx

A set of .NET MAUI controls
Apache License 2.0
225 stars 55 forks source link

Change colour on selection list #58

Open shaunwurdeman opened 5 years ago

shaunwurdeman commented 5 years ago

Is there a way to change the selection list background colour and text colour?

This is specifically in reference to iOS 13's dark mode, where the selection list automatically becomes black with white text. This clashes with the rest of a light themed app's look and feel.

Thanks in advance.

ta-yamaoka commented 5 years ago

@shaunwurdeman Since the visual pripeties of SuggestionList are not defined, To change the color, you need to use a custom render.

However, since classes related to SuggestionList are defined in private, You need to extend it yourself from the master branch for full support.

If you want to extend it yourself. You can specify the color in the following places. https://github.com/dotMorten/XamarinFormsControls/blob/master/AutoSuggestBox/Platform/NativeAutoSuggestBox.iOS.cs#L310

When using a custom render. This is an unconfirmed code.

    public class MyAutoSuggestBoxRenderer : AutoSuggestBoxRenderer
    {
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(MyAutoSuggestBox.ItemsSource))
            {
                Control.SelectionList.BackgroundColor = UIColor.White;
                SetTextColor(Control.SelectionList, UIColor.Black);
            }
        }

        private void SetTextColor(UITableView tableView, UIColor color)
        {
            var sections = tableView.NumberOfSections();
            if (sections <= 0) return;
            for (var sec = 0; sec < sections; sec++)
            {
                var rows = tableView.NumberOfRowsInSection(sec);
                if (rows <= 0) break;
                for (var row = 0; row < rows; row++)
                {
                    var path = NSIndexPath.FromRowSection(sec, row);
                    var cell = tableView.CellAt(path);

                    if (cell?.TextLabel?.Font != null)
                    {
                        cell.TextLabel.TextColor = color;
                    }
                }
            }
        }

    }
dotMorten commented 4 years ago

I'll be addressing this once the spec for darkmode support in Forms gets finished: https://github.com/xamarin/Xamarin.Forms/issues/7304

Until then, I'm a little bit reluctant to make changes to default values as that might actually prevent me from adding proper darkmode support in the future. The above suggestion should be good (there's also a sample in the app showing how to do custom styling in ios, similar to the above approach).

One could even argue this control behaves correctly in DarkMode, and it's the rest of Xamarin.Forms that doesn't ;-) Having said that, I understand the need for being able to set the colors in a easy way.