iOS11中的leftBarButtonItem setTitleTextAttributes问题 [英] leftBarButtonItem setTitleTextAttributes issue in iOS11

查看:53
本文介绍了iOS11中的leftBarButtonItem setTitleTextAttributes问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目标为iOS 9.3的应用程序.

I have an app with a deployment target of iOS 9.3.

我刚刚升级到Xcode 9.0.1,并且在所有模拟器设备和我自己的运行iOS11的iPhone7设备上都注意到了此问题.该问题不会影响运行<iOS11.

I have just upgraded to Xcode 9.0.1, and have noticed this issue across all simulator devices and my own iPhone7 device running iOS11. The issue does not impact devices running < iOS11.

我正在用自定义字体初始化左栏按钮项,如下所示(在viewDidLoad中):

I am initialising a left bar button item, with a custom font as follows (in viewDidLoad):

UIBarButtonItem *safeModeButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(toggleSafeMode)];
[safeModeButton setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"Sosa-Regular" size:31],NSFontAttributeName,
  nil]forState:UIControlStateNormal];
[self.navigationItem setLeftBarButtonItem:safeModeButton];
self.navigationItem.leftItemsSupplementBackButton = YES;

不久之后,在另一种方法中,我将条形按钮的标题设置如下:

Shortly after in another method, I set the bar button title as follows:

    self.navigationItem.leftBarButtonItem.title = @"è";

问题是,我在按钮上看到了实际的è文本,而不是应该呈现的符号.è"Sosa-Regular"字体是一个符号.

The problem is, I am seeing the actual è text on the button, rather then the symbol which should be rendered. è for the "Sosa-Regular" font is a symbol.

在升级Xcode9/iOS11之前,我以前没有这个问题.在设置标题之前,我尝试过显式设置titleTextAttributes,但是它始终只显示è.好像titleTextAttributes不是持久性的,或者在viewDidLoad外部设置标题会重置按钮的titleTextAttributes.如果我在viewDidLoad中设置标题文本,一切正常.

I previously didn't have this problem prior to the Xcode9/iOS11 upgrade. I have tried explicitly setting the titleTextAttributes before I set the title, but it always just shows the è. It's as if the titleTextAttributes is not persistent or setting the title outside viewDidLoad resets the titleTextAttributes for the button. If I set the title text in viewDidLoad, it's all working ok.

任何想法都会受到赞赏.

Any ideas would be appreciated.

推荐答案

在玩了一段时间后找到了答案.初始化 UIBarButtonItem 后不久,我将其设置为 enabled = false .

Found the answer to this after playing around for a while. Shortly after initialising the UIBarButtonItem, I set it to enabled = false.

由于我只为 UIControlStateNormal 指定了标题文本属性,因此不适用于 UIControlStateDisabled .奇怪的是,这仅是iOS11提出的.因此,添加此行可解决问题:

As I only specified the title text attributes for UIControlStateNormal, it was not applicable for UIControlStateDisabled. Strange this only came up with iOS11. So adding this line fixed the problem:

[safeModeButton setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"Sosa-Regular" size:31],NSFontAttributeName,
  nil]forState:UIControlStateDisabled];

这篇关于iOS11中的leftBarButtonItem setTitleTextAttributes问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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