Expose top tab creation methods (#8441) fixes #7808 fixes #8428
authorShane Neuville <shneuvil@microsoft.com>
Tue, 19 Nov 2019 14:19:57 +0000 (07:19 -0700)
committerRui Marinho <me@ruimarinho.net>
Tue, 19 Nov 2019 14:19:57 +0000 (14:19 +0000)
Xamarin.Forms.Platform.iOS/Renderers/IShellSectionRootHeader.cs [new file with mode: 0644]
Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRenderer.cs
Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootHeader.cs
Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRootRenderer.cs
Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj

diff --git a/Xamarin.Forms.Platform.iOS/Renderers/IShellSectionRootHeader.cs b/Xamarin.Forms.Platform.iOS/Renderers/IShellSectionRootHeader.cs
new file mode 100644 (file)
index 0000000..9da7c21
--- /dev/null
@@ -0,0 +1,11 @@
+using System;
+using UIKit;
+
+namespace Xamarin.Forms.Platform.iOS
+{
+       public interface IShellSectionRootHeader : IDisposable
+       {
+               UIViewController ViewController { get; }
+               ShellSection ShellSection { get; set; }
+       }
+}
\ No newline at end of file
index 0103790..9a949d5 100644 (file)
@@ -174,12 +174,17 @@ namespace Xamarin.Forms.Platform.iOS
                                UpdateTabBarItem();
                }
 
+               protected virtual IShellSectionRootRenderer CreateShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext)
+               {
+                       return new ShellSectionRootRenderer(shellSection, shellContext);
+               }
+
                protected virtual void LoadPages()
                {
-                       _renderer = new ShellSectionRootRenderer(ShellSection, _context);
+                       _renderer = CreateShellSectionRootRenderer(ShellSection, _context);
 
                        PushViewController(_renderer.ViewController, false);
-
+                       
                        var stack = ShellSection.Stack;
                        for (int i = 1; i < stack.Count; i++)
                        {
index e13438e..e86d112 100644 (file)
@@ -7,7 +7,7 @@ using UIKit;
 
 namespace Xamarin.Forms.Platform.iOS
 {
-       public class ShellSectionRootHeader : UICollectionViewController, IAppearanceObserver
+       public class ShellSectionRootHeader : UICollectionViewController, IAppearanceObserver, IShellSectionRootHeader
        {
                #region IAppearanceObserver
 
@@ -66,6 +66,8 @@ namespace Xamarin.Forms.Platform.iOS
                public double SelectedIndex { get; set; }
                public ShellSection ShellSection { get; set; }
 
+               public UIViewController ViewController => this;
+
                public override bool CanMoveItem(UICollectionView collectionView, NSIndexPath indexPath)
                {
                        return false;
@@ -73,7 +75,11 @@ namespace Xamarin.Forms.Platform.iOS
 
                public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
                {
-                       var headerCell = (ShellSectionHeaderCell)collectionView.DequeueReusableCell(CellId, indexPath);
+                       var reusedCell = (UICollectionViewCell)collectionView.DequeueReusableCell(CellId, indexPath);
+                       var headerCell = reusedCell as ShellSectionHeaderCell;
+
+                       if (headerCell == null)
+                               return reusedCell;
 
                        var selectedItems = collectionView.GetIndexPathsForSelectedItems();
 
@@ -96,8 +102,8 @@ namespace Xamarin.Forms.Platform.iOS
 
                public override void ItemDeselected(UICollectionView collectionView, NSIndexPath indexPath)
                {
-                       var cell = (ShellSectionHeaderCell)CollectionView.CellForItem(indexPath);
-                       cell.Label.TextColor = _unselectedColor.ToUIColor();
+                       if(CollectionView.CellForItem(indexPath) is ShellSectionHeaderCell cell)
+                               cell.Label.TextColor = _unselectedColor.ToUIColor();
                }
 
                public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
@@ -109,8 +115,8 @@ namespace Xamarin.Forms.Platform.iOS
                        if (item != ShellSection.CurrentItem)
                                ShellSection.SetValueFromRenderer(ShellSection.CurrentItemProperty, item);
 
-                       var cell = (ShellSectionHeaderCell)CollectionView.CellForItem(indexPath);
-                       cell.Label.TextColor = _selectedColor.ToUIColor();
+                       if (CollectionView.CellForItem(indexPath) is ShellSectionHeaderCell cell)
+                               cell.Label.TextColor = _selectedColor.ToUIColor();
                }
 
                public override nint NumberOfSections(UICollectionView collectionView)
@@ -164,7 +170,7 @@ namespace Xamarin.Forms.Platform.iOS
                        flowLayout.MinimumLineSpacing = 0;
                        flowLayout.EstimatedItemSize = new CGSize(70, 35);
 
-                       CollectionView.RegisterClassForCell(typeof(ShellSectionHeaderCell), CellId);
+                       CollectionView.RegisterClassForCell(GetCellType(), CellId);
 
                        ((IShellController)_shellContext.Shell).AddAppearanceObserver(this, ShellSection);
                        ((INotifyCollectionChanged)ShellSection.Items).CollectionChanged += OnShellSectionItemsChanged;
@@ -173,6 +179,11 @@ namespace Xamarin.Forms.Platform.iOS
                        ShellSection.PropertyChanged += OnShellSectionPropertyChanged;
                }
 
+               protected virtual Type GetCellType()
+               {
+                       return typeof(ShellSectionHeaderCell);
+               }
+
                protected override void Dispose(bool disposing)
                {
                        base.Dispose(disposing);
index 7d53db7..b885ae3 100644 (file)
@@ -22,7 +22,7 @@ namespace Xamarin.Forms.Platform.iOS
                UIView _blurView;
                UIView _containerArea;
                int _currentIndex;
-               ShellSectionRootHeader _header;
+               IShellSectionRootHeader _header;
                bool _isAnimating;
                Dictionary<ShellContent, IVisualElementRenderer> _renderers = new Dictionary<ShellContent, IVisualElementRenderer>();
                IShellPageRendererTracker _tracker;
@@ -197,6 +197,11 @@ namespace Xamarin.Forms.Platform.iOS
                        }
                }
 
+               protected virtual IShellSectionRootHeader CreateShellSectionRootHeader(IShellContext shellContext)
+               {
+                       return new ShellSectionRootHeader(shellContext);
+               }
+
                protected virtual void UpdateHeaderVisibility()
                {
                        bool visible = ShellSection.Items.Count > 1;
@@ -205,11 +210,11 @@ namespace Xamarin.Forms.Platform.iOS
                        {
                                if (_header == null)
                                {
-                                       _header = new ShellSectionRootHeader(_shellContext);
+                                       _header = CreateShellSectionRootHeader(_shellContext);
                                        _header.ShellSection = ShellSection;
 
-                                       AddChildViewController(_header);
-                                       View.AddSubview(_header.View);
+                                       AddChildViewController(_header.ViewController);
+                                       View.AddSubview(_header.ViewController.View);
                                }
                                _blurView.Hidden = false;
                                LayoutHeader();
@@ -218,8 +223,8 @@ namespace Xamarin.Forms.Platform.iOS
                        {
                                if (_header != null)
                                {
-                                       _header.View.RemoveFromSuperview();
-                                       _header.RemoveFromParentViewController();
+                                       _header.ViewController.View.RemoveFromSuperview();
+                                       _header.ViewController.RemoveFromParentViewController();
                                        _header.Dispose();
                                        _header = null;
                                }
@@ -267,7 +272,7 @@ namespace Xamarin.Forms.Platform.iOS
                                var headerTop = Forms.IsiOS11OrNewer ? View.SafeAreaInsets.Top : TopLayoutGuide.Length;
                                CGRect frame = new CGRect(View.Bounds.X, headerTop, View.Bounds.Width, HeaderHeight);
                                _blurView.Frame = frame;
-                               _header.View.Frame = frame;
+                               _header.ViewController.View.Frame = frame;
                        }
 
                        nfloat left;
index a95a61f..91f22fb 100644 (file)
     <Compile Include="Forms.cs" />
     <Compile Include="PageExtensions.cs" />
     <Compile Include="Renderers\IAccessibilityElementsController.cs" />
+    <Compile Include="Renderers\IShellSectionRootHeader.cs" />
     <Compile Include="Renderers\PageContainer.cs" />
     <Compile Include="Renderers\CheckBoxRendererBase.cs" />
     <Compile Include="Renderers\WkWebViewRenderer.cs" />