我在我的导航 Controller 中使用了两个图像。一个用于 rightBarButtonItem
,一个用于 titleView
。我在代码中创建了两者。所以我的问题是标题 View 不在中间,它离左边有点近。是因为右栏按钮,是肯定的,我测试了一下,发现了一些相关的问题,但是还是没能解决问题。
我尝试了 [[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault];
但没有帮助我。我真的很感激任何建议。
- (void)setupLogo {
[self setNeedsStatusBarAppearanceUpdate];
CGRect myImage = CGRectMake(0, 0, 41, 41);
UIImageView *myLogo = [[UIImageView alloc] initWithFrame:myImage];
[myLogo setImage:[UIImage imageNamed:@"logo.png"]];
myLogo.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = myLogo;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault];
}
最佳答案
titleView
位于左侧和右侧项目之间。它不以导航栏为中心。
一种可能的解决方案是添加一个与右侧栏按钮项目大小相同的虚拟左侧栏按钮项目。这将强制 titleView
居中。
关于ios - 无法将 UINavigationBar 中的图像设置到中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758098/