如何覆盖iOS的Xamarin表单选项卡页面项目字体?

如何覆盖iOS的Xamarin表单选项卡页面项目字体?,xamarin,xamarin.ios,xamarin.forms,Xamarin,Xamarin.ios,Xamarin.forms,为了使我的Xamarin表单应用程序具有一致的外观,我需要知道如何更改选项卡式页面选项卡栏图标的字体。使用iOS API建议的uitabaritem.Appearance似乎没有任何效果。需要做什么?这是一个特别有趣的问题。我试过: 外观 使用uitabaritem.Appearance.SetTitleTextAttributes方法将UITextAttribute更新为UIControlState.Normal的我的字体(大小9.0f)。这似乎没有任何区别 UINavigationBar.外

为了使我的Xamarin表单应用程序具有一致的外观,我需要知道如何更改选项卡式页面选项卡栏图标的字体。使用iOS API建议的
uitabaritem.Appearance
似乎没有任何效果。需要做什么?

这是一个特别有趣的问题。我试过:

外观 使用
uitabaritem.Appearance.SetTitleTextAttributes
方法将UITextAttribute更新为
UIControlState.Normal
的我的字体(大小9.0f)。这似乎没有任何区别

UINavigationBar.外观 我发现设置
UINavigationBar.Appearance.SetTitleTextAttributes
将更新
UINavigationBar
文本外观以及
uitabaritem
文本外观

这是一个问题,因为选项卡式页面项目字体太大

自定义
选项卡渲染器
受我在某处看到的一个示例的启发,我在iOS项目中将
tabbedderenderer
子类化

我尝试覆盖
选项卡渲染器的设置程序。选择ViewController
属性并循环通过
ViewController
属性来设置它们的项。图标将以标准字体显示,但一旦用户更改选项卡,它们将全部更新为所需字体。快到了

然后,我尝试覆盖
AddChildViewController
,并在添加该控制器后更新该控制器的
TabBarItem
,结果没有效果。添加控制器后,添加页面的
TabBarItem
会在某个时间点更新

最终,我发现覆盖
视图将出现
,并在当时设置所有选项卡栏项目的外观似乎可以完成我想要的工作


我在中包含了一个示例。

您需要编写这样的自定义渲染器,请从下面的代码中获取线索!它有你想要的

 [assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(TabbedPageCustom))]
    namespace App.iOS
    {
        public class TabbedPageCustom : TabbedRenderer
        {
            public TabbedPageCustom ()
        {
            TabBar.TintColor = UIKit.UIColor.White;
            TabBar.BarTintColor = UIKit.UIColor.White;
            TabBar.BackgroundColor = UIKit.UIColor.Red;
        }
        protected override void OnElementChanged (VisualElementChangedEventArgs e)
        {
            base.OnElementChanged (e);

            // Set Text Font for unselected tab states
            UITextAttributes normalTextAttributes = new UITextAttributes();
            normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Light", 20.0F); // unselected
            normalTextAttributes.TextColor = UIKit.UIColor.Blue;

            UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
        }
        public override UIViewController SelectedViewController {
            get {
                UITextAttributes selectedTextAttributes = new UITextAttributes();
                selectedTextAttributes.Font = UIFont.FromName("ChalkboardSE-Bold", 20.0F); // SELECTED
                if (base.SelectedViewController != null)
                {
                    base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);
                }
                return base.SelectedViewController;
            }
            set {
                base.SelectedViewController = value;

                foreach (UIViewController viewController in base.ViewControllers)
                {
                    UITextAttributes normalTextAttributes = new UITextAttributes();
                    normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Light", 20.0F); // unselected
                    normalTextAttributes.TextColor = UIKit.UIColor.Blue;
                    viewController.TabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
                }
            }
        }

    }
}

感谢您的回复,但正如我在这篇问答文章的答案部分所指出的,仅仅设置
uitabaritem.Appearance.SetTitleTextAttributes
并不是出于任何原因。Xamarin Forms不尊重该设置。我给您的代码是在ios9上工作的!这是对我的尊重,如果你愿意,我可以给你看屏幕截图。