【发布时间】:2021-03-11 17:41:08
【问题描述】:
我正在尝试在 Xamarin iOS (13+) 应用程序中设置导航按钮(特别是后退按钮)的字体。我发现这个 this solution 声称可以工作但没有,至少对我来说:
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;
然后我将它简化了一点(看看我是否可以简单地重新着色导航栏标题)但这又什么也没做:
var style = new UINavigationBarAppearance();
var a = new UIStringAttributes()
{
ForegroundColor = UIColor.Yellow
};
style.TitleTextAttributes = a;
UINavigationBar.Appearance.StandardAppearance = style;
玩得更远,我奇怪地发现,以下内容确实会重新着色导航栏标题:
var a = new UIStringAttributes()
{
ForegroundColor = UIColor.Yellow
};
UINavigationBar.Appearance.TitleTextAttributes = a;
...但是使用相同的思考过程,这不会重新着色导航栏按钮:
var a = new UITextAttributes()
{
TextColor = UIColor.Yellow
};
UIBarButtonItem.Appearance.SetTitleTextAttributes(a, UIControlState.Normal);
谁能明白为什么这些方法都不能用于修改导航栏按钮的样式?或者这可能是 Xamarin 的问题?
更新:我意识到第一个示例确实有效,但仅在 Xamarin iOS 中。但是,它在 Xamarin Forms 中不起作用。看起来 Xamarin Forms 可能会强制执行其自己的标准外观,这会覆盖我在代码中输入的内容。
【问题讨论】:
标签: ios xamarin.forms