您如何访问 Shell 页面上 iOS 的前 tab/toolbar?
How do you access the top tab/toolbar for iOS on Shell pages?
我一直在尝试设置顶部 tab/toolbar 的重力/定位选项(将两个 Shell 内容设置到一个选项卡时出现的布局)。这在 Android 上有效,因为 Android Shell 渲染器公开了 CreateBottomNavViewAppearanceTracker
和 CreateTabLayoutAppearanceTracker
。
但是,iOS Shell 渲染器仅公开 CreateTabBarAppearanceTracker
关于仅处理(至少从我的理解)底部选项卡(层次高于 Shell内容选项卡)。
我曾尝试子类化 ShellItemRenderer
,但我找不到任何与我想要的相关的属性。
它在 Android 上的显示方式:
我找到了访问 Shell 页面上 iOS 的顶部 tab/toolbar 的解决方案。您需要 ShellSectionRootHeader
的子类,它本身是 UICollectionViewController
的子类,UICollectionViewController
是在 iOS 上呈现顶部标签栏的本机类型。拥有该子类后,您可以覆盖 GetCell
方法以获取顶部选项卡栏中的每个单独项目并修改它们。但是为了达到这一点,您需要在 ShellRenderer
.
之外子类化并实例化另外 2 个渲染器
这是 iOS 中的示例渲染器代码:
[assembly: ExportRenderer(typeof(Shell), typeof(CustomShellRenderer))]
namespace Xaminals.iOS
{
public class CustomShellRenderer: ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var shellSectionRenderer = new CustomShellSectionRenderer(this);
return shellSectionRenderer;
}
}
public class CustomShellSectionRenderer : ShellSectionRenderer
{
public CustomShellSectionRenderer(IShellContext context) : base(context)
{ }
protected override IShellSectionRootRenderer CreateShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext)
{
var renderer = new CustomShellSectionRootRenderer(shellSection, shellContext);
return renderer;
}
}
public class CustomShellSectionRootRenderer : ShellSectionRootRenderer
{
public CustomShellSectionRootRenderer(ShellSection section, IShellContext context) : base(section, context)
{ }
protected override IShellSectionRootHeader CreateShellSectionRootHeader(IShellContext shellContext)
{
var renderer = new CustomShellSectionRootHeader(shellContext);
return renderer;
}
}
public class CustomShellSectionRootHeader : ShellSectionRootHeader
{
public CustomShellSectionRootHeader(IShellContext context) : base(context)
{
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = base.GetCell(collectionView, indexPath) as ShellSectionHeaderCell;
// Set desired font here
//cell.Label.Font = UIFont.ItalicSystemFontOfSize(11);
var layout = new UICollectionViewFlowLayout();
layout.MinimumInteritemSpacing = UIScreen.MainScreen.Bounds.Size.Width / 8;
layout.SectionInset = new UIEdgeInsets(top: 0, left: UIScreen.MainScreen.Bounds.Size.Width/4, bottom: 0, right: 0);
collectionView.CollectionViewLayout = layout;
return cell;
}
}
public class CustomUICollectionViewFlowLayout : UICollectionViewFlowLayout
{
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
return base.LayoutAttributesForElementsInRect(rect);
}
}
}
效果:
我一直在尝试设置顶部 tab/toolbar 的重力/定位选项(将两个 Shell 内容设置到一个选项卡时出现的布局)。这在 Android 上有效,因为 Android Shell 渲染器公开了 CreateBottomNavViewAppearanceTracker
和 CreateTabLayoutAppearanceTracker
。
但是,iOS Shell 渲染器仅公开 CreateTabBarAppearanceTracker
关于仅处理(至少从我的理解)底部选项卡(层次高于 Shell内容选项卡)。
我曾尝试子类化 ShellItemRenderer
,但我找不到任何与我想要的相关的属性。
它在 Android 上的显示方式:
我找到了访问 Shell 页面上 iOS 的顶部 tab/toolbar 的解决方案。您需要 ShellSectionRootHeader
的子类,它本身是 UICollectionViewController
的子类,UICollectionViewController
是在 iOS 上呈现顶部标签栏的本机类型。拥有该子类后,您可以覆盖 GetCell
方法以获取顶部选项卡栏中的每个单独项目并修改它们。但是为了达到这一点,您需要在 ShellRenderer
.
这是 iOS 中的示例渲染器代码:
[assembly: ExportRenderer(typeof(Shell), typeof(CustomShellRenderer))]
namespace Xaminals.iOS
{
public class CustomShellRenderer: ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var shellSectionRenderer = new CustomShellSectionRenderer(this);
return shellSectionRenderer;
}
}
public class CustomShellSectionRenderer : ShellSectionRenderer
{
public CustomShellSectionRenderer(IShellContext context) : base(context)
{ }
protected override IShellSectionRootRenderer CreateShellSectionRootRenderer(ShellSection shellSection, IShellContext shellContext)
{
var renderer = new CustomShellSectionRootRenderer(shellSection, shellContext);
return renderer;
}
}
public class CustomShellSectionRootRenderer : ShellSectionRootRenderer
{
public CustomShellSectionRootRenderer(ShellSection section, IShellContext context) : base(section, context)
{ }
protected override IShellSectionRootHeader CreateShellSectionRootHeader(IShellContext shellContext)
{
var renderer = new CustomShellSectionRootHeader(shellContext);
return renderer;
}
}
public class CustomShellSectionRootHeader : ShellSectionRootHeader
{
public CustomShellSectionRootHeader(IShellContext context) : base(context)
{
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = base.GetCell(collectionView, indexPath) as ShellSectionHeaderCell;
// Set desired font here
//cell.Label.Font = UIFont.ItalicSystemFontOfSize(11);
var layout = new UICollectionViewFlowLayout();
layout.MinimumInteritemSpacing = UIScreen.MainScreen.Bounds.Size.Width / 8;
layout.SectionInset = new UIEdgeInsets(top: 0, left: UIScreen.MainScreen.Bounds.Size.Width/4, bottom: 0, right: 0);
collectionView.CollectionViewLayout = layout;
return cell;
}
}
public class CustomUICollectionViewFlowLayout : UICollectionViewFlowLayout
{
public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(CGRect rect)
{
return base.LayoutAttributesForElementsInRect(rect);
}
}
}
效果: