如何使用 Xamarin Forms Shell 在 Tabbar 行和 iOS 中的图标之间添加 space?
How to add a space between the Tabbar line and the icons in iOS using Xamarin Forms Shell?
我使用了 ShellRenderer,但我无法让代码正常工作以在 Tabbar 行和图标之间添加额外的 space。这是一张图片:
我需要增加灰线和所有图标顶部之间的 space。
这是我试过的代码。
[assembly: ExportRenderer(typeof(Shell), typeof(CustomShellRenderer))]
namespace MyProject.iOS.CustomRenderers
{
public class CustomShellRenderer : ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var renderer = base.CreateShellSectionRenderer(shellSection);
if (renderer != null)
{
var a = (renderer as ShellSectionRenderer);
(renderer as ShellSectionRenderer).NavigationBar.Translucent = false;
}
return renderer;
}
protected override IShellItemRenderer CreateShellItemRenderer(ShellItem item)
{
var renderer = base.CreateShellItemRenderer(item);
(renderer as ShellItemRenderer).TabBar.Translucent = false;
(renderer as ShellItemRenderer).TabBar.ShadowImage = new UIImage();
(renderer as ShellItemRenderer).TabBar.BackgroundImage = new UIImage();
CGRect frame = (renderer as ShellItemRenderer).TabBar.Frame;
UIView view = new UIView();
view.BackgroundColor = UIColor.Yellow;
view.Frame = new CGRect(frame.X, frame.Y, frame.Width, 10);
(renderer as ShellItemRenderer).TabBar.AddSubview(view);
return renderer;
}
}
您可以添加一点 ImageInsets
的 barItem 以在灰线和所有图标的顶部之间创建一个 space。您不能添加额外的视图,因为 space 是固定的。
public class MyShellRenderer : ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var renderer = base.CreateShellSectionRenderer(shellSection);
if (renderer != null)
{
(renderer as ShellSectionRenderer).NavigationBar.SetBackgroundImage(UIImage.FromFile("monkey.png"), UIBarMetrics.Default);
}
return renderer;
}
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new CustomTabbarAppearance();
}
}
public class CustomTabbarAppearance : IShellTabBarAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(UITabBarController controller)
{
}
public void UpdateLayout(UITabBarController controller)
{
UITabBar myTabBar = controller.TabBar;
foreach (var barItem in myTabBar.Items)
{
barItem.ImageInsets = new UIEdgeInsets(8, 0, 0, 0);
//barItem.TitlePositionAdjustment = new UIOffset(10, 0);
}
}
}
你可以查看我的样本here。
我使用了 ShellRenderer,但我无法让代码正常工作以在 Tabbar 行和图标之间添加额外的 space。这是一张图片:
我需要增加灰线和所有图标顶部之间的 space。 这是我试过的代码。
[assembly: ExportRenderer(typeof(Shell), typeof(CustomShellRenderer))]
namespace MyProject.iOS.CustomRenderers
{
public class CustomShellRenderer : ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var renderer = base.CreateShellSectionRenderer(shellSection);
if (renderer != null)
{
var a = (renderer as ShellSectionRenderer);
(renderer as ShellSectionRenderer).NavigationBar.Translucent = false;
}
return renderer;
}
protected override IShellItemRenderer CreateShellItemRenderer(ShellItem item)
{
var renderer = base.CreateShellItemRenderer(item);
(renderer as ShellItemRenderer).TabBar.Translucent = false;
(renderer as ShellItemRenderer).TabBar.ShadowImage = new UIImage();
(renderer as ShellItemRenderer).TabBar.BackgroundImage = new UIImage();
CGRect frame = (renderer as ShellItemRenderer).TabBar.Frame;
UIView view = new UIView();
view.BackgroundColor = UIColor.Yellow;
view.Frame = new CGRect(frame.X, frame.Y, frame.Width, 10);
(renderer as ShellItemRenderer).TabBar.AddSubview(view);
return renderer;
}
}
您可以添加一点 ImageInsets
的 barItem 以在灰线和所有图标的顶部之间创建一个 space。您不能添加额外的视图,因为 space 是固定的。
public class MyShellRenderer : ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var renderer = base.CreateShellSectionRenderer(shellSection);
if (renderer != null)
{
(renderer as ShellSectionRenderer).NavigationBar.SetBackgroundImage(UIImage.FromFile("monkey.png"), UIBarMetrics.Default);
}
return renderer;
}
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new CustomTabbarAppearance();
}
}
public class CustomTabbarAppearance : IShellTabBarAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(UITabBarController controller)
{
}
public void UpdateLayout(UITabBarController controller)
{
UITabBar myTabBar = controller.TabBar;
foreach (var barItem in myTabBar.Items)
{
barItem.ImageInsets = new UIEdgeInsets(8, 0, 0, 0);
//barItem.TitlePositionAdjustment = new UIOffset(10, 0);
}
}
}
你可以查看我的样本here。