thi*_*sai 4 fonts uinavigationbar ios uiappearance
从iOS 6开始,我在应用程序中使用自定义样式时遇到了一些问题.我使用自定义字体和几个UIAppearance代理.我无法理解的一个问题是UINavigationBar中标题的错位.在iOS 5中,一切正常,并且正确对齐.
由于iOS6已经发布并且自定义样式并不少见,我认为这不是一个bug,而是我对iOS6的一些新变化的误解.

我在文档中搜索了一个文本对齐方法来调用UIAppearance代理,但我无法找到这样的方法.
我使用以下代码行来为我的整个应用程序设置UINavigationBar的样式:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"]
forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:@"Corbel-Bold" size:14.0], UITextAttributeFont,
nil]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor ceBlueColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Corbel" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -3) forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
小智 12
同样的问题在这里,但我注意到当导航栏有后退按钮或右键按钮(表编辑)时不会发生这种情况.导航到新的视图控制器然后返回第一个时,标题上的对齐也是固定的...
我认为正在发生的是iOS根据默认字体计算标题框架的位置,因为我使用的字体稍微偏小,标题偏向中心左侧.
我目前的修复是setNeedsLayout在viewWillAppear中调用.似乎工作.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.navigationItem.hidesBackButton || self.navigationItem.rightBarButtonItem == nil) {
[self.navigationController.navigationBar setNeedsLayout];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果有帮助,您可以调整(至少)标题的垂直位置:
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:4 forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
(这个问题帮助我找到了解决这个问题的方法:UINavigationBar自定义标题位置)
| 归档时间: |
|
| 查看次数: |
4673 次 |
| 最近记录: |