From d4920022f6fc47648155533e81c2a466fc79feac Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 21 Nov 2019 08:33:05 -0700 Subject: [PATCH] [iOS] Fix top tab highlighting (#8599) * Let UICollectionViewCell handle state itself (#7884) Fixes #7416 * [iOS] Fix top tab highlighting --- .../Issue6362.cs | 55 ++++++++++++++++++++++ .../TestPages/TestPages.cs | 9 +++- .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + .../Renderers/ShellSectionRootHeader.cs | 27 +++++++---- 4 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6362.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6362.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6362.cs new file mode 100644 index 0000000..06188cf --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6362.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +using Xamarin.Forms.Core.UITests; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 6362, "[iOS] Shell GoToAsync doesn't update selected tab to bold", + PlatformAffected.iOS)] +#if UITEST + [NUnit.Framework.Category(UITestCategories.Shell)] +#endif + public class Issue6362 : TestShell + { + protected override void Init() + { + Shell.SetFlyoutBehavior(this, FlyoutBehavior.Disabled); + AddTopTab( + new ContentPage() + { + Content = new StackLayout() + { + Children = + { + new Label() + { + Text = "Click between tabs and make sure the tab colors change" + }, + new Button() + { + Text = "Go to tab 2 and make sure tab colors change", + Command = new Command(()=> + { + GoToAsync(@"\\\Second"); + }) + } + } + } + }, + "First"); + + AddTopTab("Second"); + } + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs index 605b1ac..677f92b 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs @@ -619,11 +619,16 @@ namespace Xamarin.Forms.Controls return; } - Items[0].Items[0].Items.Add(new ShellContent() + var content = new ShellContent() { Title = title ?? page.Title, Content = page - }); + }; + + Items[0].Items[0].Items.Add(content); + + if (!String.IsNullOrWhiteSpace(content.Title)) + content.Route = content.Title; } public ContentPage AddBottomTab(string title) diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index c002b24..d1bfc4a 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootHeader.cs b/Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootHeader.cs index e13438e..8d676de 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootHeader.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootHeader.cs @@ -81,10 +81,13 @@ namespace Xamarin.Forms.Platform.iOS headerCell.Label.Text = shellContent.Title; headerCell.Label.SetNeedsDisplay(); + headerCell.SelectedColor = _selectedColor.ToUIColor(); + headerCell.UnSelectedColor = _unselectedColor.ToUIColor(); + if (selectedItems.Length > 0 && selectedItems[0].Row == indexPath.Row) - headerCell.Label.TextColor = _selectedColor.ToUIColor(); + headerCell.Selected = true; else - headerCell.Label.TextColor = _unselectedColor.ToUIColor(); + headerCell.Selected = false; return headerCell; } @@ -96,21 +99,14 @@ namespace Xamarin.Forms.Platform.iOS public override void ItemDeselected(UICollectionView collectionView, NSIndexPath indexPath) { - var cell = (ShellSectionHeaderCell)CollectionView.CellForItem(indexPath); - cell.Label.TextColor = _unselectedColor.ToUIColor(); } public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath) { var row = indexPath.Row; - var item = ShellSection.Items[row]; - if (item != ShellSection.CurrentItem) ShellSection.SetValueFromRenderer(ShellSection.CurrentItemProperty, item); - - var cell = (ShellSectionHeaderCell)CollectionView.CellForItem(indexPath); - cell.Label.TextColor = _selectedColor.ToUIColor(); } public override nint NumberOfSections(UICollectionView collectionView) @@ -229,6 +225,19 @@ namespace Xamarin.Forms.Platform.iOS public class ShellSectionHeaderCell : UICollectionViewCell { + public UIColor SelectedColor { get; set; } + public UIColor UnSelectedColor { get; set; } + + public override bool Selected + { + get => base.Selected; + set + { + base.Selected = value; + Label.TextColor = value ? SelectedColor : UnSelectedColor; + } + } + [Export("initWithFrame:")] public ShellSectionHeaderCell(CGRect frame) : base(frame) { -- 2.7.4