Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change colour on selection list #58

Open
shaunwurdeman opened this issue Nov 25, 2019 · 2 comments
Open

Change colour on selection list #58

shaunwurdeman opened this issue Nov 25, 2019 · 2 comments
Labels
F-AutoSuggestBox Feature: AutoSuggestBox control

Comments

@shaunwurdeman
Copy link

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
Copy link

@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
Copy link
Owner

dotMorten commented Feb 7, 2020

I'll be addressing this once the spec for darkmode support in Forms gets finished: xamarin/Xamarin.Forms#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.

@dotMorten dotMorten added the F-AutoSuggestBox Feature: AutoSuggestBox control label Mar 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-AutoSuggestBox Feature: AutoSuggestBox control
Projects
None yet
Development

No branches or pull requests

3 participants