在 Xamarin Forms iOS 13+ 应用程序中设置导航栏按钮上的字体 [英] Setting the font on navigation bar buttons in a Xamarin Forms iOS 13+ app

查看:29
本文介绍了在 Xamarin Forms iOS 13+ 应用程序中设置导航栏按钮上的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Xamarin iOS (13+) 应用中的导航按钮(特别是后退按钮)设置字体.我发现这个这个解决方案声称工作但没有,至少对我来说:

I'm trying to set the font for navigation buttons (specifically the Back button) in Xamarin iOS (13+) apps. I found this this solution which claims to work but didn't, for me at least:

var style = new UINavigationBarAppearance();

var a = new UIStringAttributes();
a.Font = UIFont.SystemFontOfSize(35);
a.ForegroundColor = UIColor.Yellow;
style.TitleTextAttributes = a;

var dic = new NSDictionary<NSString, NSObject>(
            new NSString[] {a.Dictionary.Keys[0] as NSString, a.Dictionary.Keys[1] as NSString }, a.Dictionary.Values
);
   
var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain);
button.Normal.TitleTextAttributes = dic;
button.Highlighted.TitleTextAttributes = dic;
style.ButtonAppearance = button;

UINavigationBar.Appearance.StandardAppearance = style;

然后我稍微简化了它(看看我是否可以简单地重新着色导航栏标题)但是这再次没有做任何事情:

I then simplified it a little (seeing if I could simply recolour the nav bar title) but this again did nothing:

var style = new UINavigationBarAppearance();

var a = new UIStringAttributes()
{
    ForegroundColor = UIColor.Yellow
};
style.TitleTextAttributes = a;

UINavigationBar.Appearance.StandardAppearance = style;

我进一步探索,奇怪的是,以下内容确实为导航栏标题重新着色:

Playing around further I found, curiously, the following DOES recolour the navigation bar title:

var a = new UIStringAttributes()
{
    ForegroundColor = UIColor.Yellow
};

UINavigationBar.Appearance.TitleTextAttributes = a;

...但使用相同的思维过程,这不会重新着色导航栏按钮:

...but using the same thought process, this does NOT recolour the navigation bar buttons:

var a = new UITextAttributes()
{
    TextColor = UIColor.Yellow
};

UIBarButtonItem.Appearance.SetTitleTextAttributes(a, UIControlState.Normal);

谁能明白为什么这些方法都不能用于修改导航栏按钮样式?或者这可能是 Xamarin 的问题?

Can anyone see why none of these approaches work for modifying the navigation bar button styling? Or might this be an issue with Xamarin?

更新:我意识到第一个示例确实有效,但仅适用于 Xamarin iOS.但是,它在 Xamarin Forms 中不起作用.看起来 Xamarin Forms 可能会强制执行自己的标准外观,它会覆盖我在代码中放置的内容.

UPDATE: I realised the first sample does work, but only in Xamarin iOS. It does not work in Xamarin Forms however. It looks like Xamarin Forms might be enforcing its own standard appearance which overrides what I put in my code.

推荐答案

我终于找到了一种方法,该方法通过覆盖 Xamarin Forms 不断执行其自己的导航栏样式来起作用:

I finally figured out an approach that works by overriding Xamarin Forms' constant enforcement of its own navigation bar styling:

public class MyNavigationRenderer : NavigationRenderer
{
    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);

        UpdateStyle();

        Element.PropertyChanged += HandlePropertyChanged;
    }

    void UpdateStyle()
    {
        var navigationBarAppearance = NavigationBar.StandardAppearance;
        var titleTextAttributes = new UIStringAttributes
        {
            ForegroundColor = UIColor.Yellow,
            Font = UIFont.SystemFontOfSize(35)
        };

        var dic = new NSDictionary<NSString, NSObject>(
            new NSString[] { titleTextAttributes.Dictionary.Keys[0] as NSString, titleTextAttributes.Dictionary.Keys[1] as NSString }, titleTextAttributes.Dictionary.Values
        );

        var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain);
        button.Normal.TitleTextAttributes = dic;
        button.Highlighted.TitleTextAttributes = dic;

        navigationBarAppearance.ButtonAppearance = button;

        NavigationBar.StandardAppearance = navigationBarAppearance;
        NavigationBar.CompactAppearance = navigationBarAppearance;
        NavigationBar.ScrollEdgeAppearance = navigationBarAppearance;
    }

    void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        UpdateStyle();
    }
}

这篇关于在 Xamarin Forms iOS 13+ 应用程序中设置导航栏按钮上的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆