我正在尝试自定义我的UINavigationBar字体,在我的app delegate中使用以下iOS 5代码application:didFinishLaunchingWithOptions:
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)])
{
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:kDefaultFont size:0.0], UITextAttributeFont,
nil]];
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,导航栏使用我的字体呈现.大.
我发现的参考文献建议您可以使用零字体大小,它将调整字体大小以适合您的导航栏(使用稍小的字体用于横向布局的较短导航栏).它确实选择了适合导航栏高度的字体大小.但看起来如果你从纵向转向横向和背面,导航栏的标题标签的宽度会被搞砸,所以当你第一次看到它时,显示为"Long Titlebar"的标题看起来很好在纵向方向,当你在横向中查看时看起来很好(使用相应较小的字体),但当我回到肖像时,字体正确地恢复为较大的字体,但标题文本本身被截断,变为"长... "尽管有足够的空间可以获得完整的冠军头衔.当使用0.0的字体大小时,是否有其他人看到过这种行为?
很显然,我可以指定实际的字体大小(在这种情况下,我不认为这截断行为),但后来我手动搞清楚使用什么尺寸.更糟糕的是,横向和纵向的字体大小相同,所以现在我使用的字体大小适合较短的横向导航栏标题,并且标题小于它需要在更高的纵向导航栏中.
有人在那里有使用setTitleTextAttributes改变字体的经验[UINavigationBar appearance],使得字体大小在纵向和横向之间变化,但是当你去景观后返回肖像时没有标题的截断吗?我即将寻求各种麻烦的解决方法,但如果您对此问题有任何经验,请告诉我.
更新:
在向Apple提交此错误的过程中,我决定演示如何重现该问题:
在Xcode 4.3.2中创建新的iOS Master-Detail应用程序.
将上面的setTitleTextAttributes代码放在app delegate中application:didFinishLaunchingWithOptions(我使用了字体@"GillSans").
转到MasterViewController并添加说明的行 self.title = @"Long Title";
注释掉UIBarButtonItem *addButton代码.
运行程序.注意标题正确地说"长标题".旋转到风景.看起来还不错.旋转回肖像,标题现在显示"长......",即使有足够的空间.
奇怪的是,如果您恢复UIBarButtonItem *addButton代码,标题将按预期工作.但是,如果您要么取消UIBarButton项目,或者使用initWithTitle而不是使用的按钮替换它,则initWithBarButtonSystemItem在从纵向旋转到横向然后再回到纵向之后,您会遇到导航栏标题的问题.
以下是我针对此问题的解决方法,这是在每种情况下我的解决方法实现旁边出现的注释,以提醒我自己为什么要实现这段代码:
\n\n// when using an appearance proxy to set a custom font for the navigation bar (generally in\n// application:didFinishLaunchingWithOptions: in the appDelegate code) for both iOS 5 & 6,\n// there's a glitch that incorrectly auto-truncates the title in the following cirumstances:\n//\n// 1) when a 0.0 value is used for UITextAttributeFont in the titleTextAttributes dictionary\n// and a device/simulator running pre-iOS 5 rotates back to portrait from landscape\n// solution: perform [self.navigationController.navigationBar setNeedsLayout] in\n// didRotateFromInterfaceOrientation: the view controller in which the\n// auto-truncation is incorrectly occurring for systemVersion < 6.0\n//\n// 2) when a view initially loads running iOS 6 for a non-0.0 value for the UITextAttributeFont\n// in the titleTextAttributes dictionary\n// solution: perform [self.navigationController.navigationBar setNeedsLayout]\n// in viewDidLoad in the view controller in which the auto truncation is\n// incorrectly occurring for systemVersion >= 6.0\nRun Code Online (Sandbox Code Playgroud)\n\n因此,对于我可以使用 0.0 值UITextAttributeFont但必须继续支持 iOS5 的情况,我使用解决方案(1):
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0\n- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {\n if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)\n && UIDevice.currentDevice.systemVersion.floatValue < 6.0)\n [self.navigationController.navigationBar setNeedsLayout];\n}\n#endif\nRun Code Online (Sandbox Code Playgroud)\n\n在几个遗留代码的情况下,我想支持 iOS 6 并修复视图首次出现时的故障,而不必重新编写 MyAppAppearance 类方法,我为我的设置设置了非 0.0 值[UINavigationBar appearance] titleTextAttributes更容易从而实现解决方案(2):
- (void)viewDidLoad\n{\n [super viewDidLoad];\n#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0\n if (UIDevice.currentDevice.systemVersion.floatValue >= 6.0)\n#endif\n [self.navigationController.navigationBar setNeedsLayout];\n\n // \xe2\x80\xa6\xc2\xa0other viewDidLoadCode\nRun Code Online (Sandbox Code Playgroud)\n\n(以及__IPHONE_OS_VERSION_MIN_REQUIRED只是帮助提醒我,如果将来需要的话,哪些代码最终可以消失,哪些可能必须保留。)
要更准确地了解此处发生的情况,特别是在旋转的情况下,请在切换慢速动画的情况下运行模拟器。
\n| 归档时间: |
|
| 查看次数: |
5063 次 |
| 最近记录: |