我在UINavigationBar中有一个UIBarButtonItem,它具有一些自定义外观属性(自定义字体、颜色)。它通常出现在整个应用程序的大多数地方。
但是,当具有ActionSheet样式的UIAlertController显示在具有此按钮的视图控制器上时,BarButton将失去其自定义字体外观,导致按钮上出现奇怪的"跳转",并显示不正确的字体,如图所示:http://giphy.com/gifs/puopiq9mPyI2Q/html5。
我认为这可能与我们如何设置BarButton外观有关,AppDelegate中有以下代码:
NSDictionary *btnAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[AppearanceConstants mediumFontSize20], NSFontAttributeName,nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:btnAttributes forState:UIControlStateNormal];
我的猜测是,条形按钮不在UIControlStateNormal中,但是,当AlertController出现时,我似乎根本无法判断条形按钮处于什么状态,仅仅试图将标题文本属性设置为每个控件状态的适当值也不起作用。
完成此操作后,当我导航到导航栏中有BarButtonItems的其他视图控制器时,它们仍然具有正确的外观,如果之后返回到有问题的屏幕,它将具有正确的外表,直到AlertController再次出现。
我在展示ActionSheet 后重置样式
UIAlertController *alertVC = ...
[self presentViewController:alertVC animated:YES completion:^{
[self.navigationItem.leftBarButtonItem setDefaultAppearanceForSpecificItem];
[self.navigationItem.rightBarButtonItem setDefaultAppearanceForSpecificItem];
}];
我在UIBarButtonItem
上创建了一个类别来帮助我
[self setTitleTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor VKBlueColor]
}
forState:UIControlStateNormal];