[iOS] Fix top tab highlighting (#8599)
authorShane Neuville <shneuvil@microsoft.com>
Thu, 21 Nov 2019 15:33:05 +0000 (08:33 -0700)
committerGitHub <noreply@github.com>
Thu, 21 Nov 2019 15:33:05 +0000 (08:33 -0700)
* Let UICollectionViewCell handle state itself (#7884)

Fixes #7416

* [iOS] Fix top tab highlighting

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6362.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/TestPages/TestPages.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootHeader.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 (file)
index 0000000..06188cf
--- /dev/null
@@ -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");
+               }
+       }
+}
index 605b1ac..677f92b 100644 (file)
@@ -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)
index c002b24..d1bfc4a 100644 (file)
@@ -10,6 +10,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="$(MSBuildThisFileDirectory)CollectionViewGroupTypeIssue.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue6362.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7505.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)CollectionViewItemsSourceTypes.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue1455.xaml.cs">
index e13438e..8d676de 100644 (file)
@@ -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)
                        {