2015-01-03 34 views
0

我在导航控制器中使用了两个图像。一个用于rightBarButtonItem,另一个用于titleView。我在代码中创建了两个。所以我的问题是,标题视图不在中心位置,它靠近左侧。这是因为正确的bar按钮,我敢肯定,我测试了它并发现了一些相关的问题,但仍然无法解决问题。无法将UINavigationBar中的图像设置为中心

我试过[[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]; 

} 

回答

2

titleView居中左右项目之间。它不在导航栏上居中。

一种可能的解决方案是添加一个与您的右栏按钮项目大小相同的虚拟左栏按钮项目。这将迫使titleView居中。