导航栏后退按钮,文本不是中心alligined

fib*_*chi 7 iphone cocoa-touch uinavigationbar ipad ios5

我使用外观代理自定义导航栏中的后退按钮,还设置图像和文本属性.一切正常,但后退按钮中的文字不是中心对齐的.

这是我的代码.

UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
 UIImage *buttonBack32 = [[UIImage imageNamed:@"NavigationBackButton"] 
                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 5)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack32 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
 [[UIBarButtonItem appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor], 
        UITextAttributeTextColor, 
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
        UITextAttributeTextShadowColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
        UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"xxx" size:16.0], 
  UITextAttributeFont,
  nil] 
                                            forState:UIControlStateNormal]; 
self.navigationItem.backBarButtonItem = backButton;
Run Code Online (Sandbox Code Playgroud)

如果我将字体大小设置为"0.0",则文本的大小会变得太小而且仍然没有居中对齐.

在此先感谢您的帮助.

小智 17

您可以使用setTitlePositionAdjustment:forBarMetrics:方法根据需要设置标题偏移量.例如:

[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 5.0f) forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)