leftBarButtonItem setTitleTextAttributes issue in iOS11

為{幸葍}努か 提交于 2019-12-24 00:46:21

问题


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

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.

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 = @"è";

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.

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.


回答1:


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

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];


来源:https://stackoverflow.com/questions/46956799/leftbarbuttonitem-settitletextattributes-issue-in-ios11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!