gpt4 book ai didi

ios - 自 iOS6 以来,UINavigationBar 中的标题未对齐

转载 作者:可可西里 更新时间:2023-11-01 05:01:45 26 4
gpt4 key购买 nike

自从 iOS 6 以来,我在应用程序中使用自定义样式时遇到了一些问题。我使用自定义字体和多个 UIAppearance 代理。我无法解决的一个问题是我的 UINavigationBar 中的标题未对齐。在 iOS 5 中一切正常并且正确对齐。

由于 iOS6 已经发布并且自定义样式并不少见,我认为这不是错误,而是我对 iOS6 的一些新变化的误解。

misalignment

我在文档中搜索了调用 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];

最佳答案

同样的问题,但我注意到当导航栏有后退按钮或右栏按钮(表格编辑)时不会发生这种情况。当导航到一个新的 View Controller 然后返回到第一个 View Controller 时,标题上的对齐方式也是固定的...

我认为发生的事情是 iOS 根据默认字体计算标题框架的位置,因为我使用的字体有点小,标题稍微偏向中心左侧。

我当前的修复是在 viewWillAppear 中调用 setNeedsLayout。似乎在工作。

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

if (self.navigationItem.hidesBackButton || self.navigationItem.rightBarButtonItem == nil) {
[self.navigationController.navigationBar setNeedsLayout];
}
}

关于ios - 自 iOS6 以来,UINavigationBar 中的标题未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com