2012-06-04 27 views
7

我使用外觀代理自定義導航欄中的後退按鈕,並設置圖像和文本屬性。一切工作正常,但後退按鈕中的文本不居中對齊。導航欄後退按鈕,文字未居中

這是我的代碼。

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; 

如果我將字體大小設置爲「0.0」,那麼文字變得太小而仍然不居中對齊。

在此先感謝您的幫助。

+0

你指的是垂直對齊或水平對齊? –

+0

我認爲嘗試這可能有助於不sur,但我在textview中做這件事情,也標籤,但不在BarButton,所以我只是嘗試,然後發佈答案[[UIBarButtonItem appearance] setTextAlignment:UITextAlignmentCenter]; :) –

+0

ParasJoshi:它沒有奏效。 – fibnochi

回答

17

您可以使用setTitlePositionAdjustment:forBarMetrics:方法根據需要設置標題偏移量。例如:

[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 5.0f) forBarMetrics:UIBarMetricsDefault]; 
+0

爲我工作(y) –

-4
You can use this : 

UIView* leftContainer = [[UIView alloc] initWithFrame:CGRectMake(0.0, 4.0, 70.0, 44.0)]; 
UIImage *profileImg = [UIImage imageNamed:@"account-btn.png"]; 
UIButton *profilebutton = [UIButton buttonWithType:UIButtonTypeCustom]; 
profilebutton.frame = CGRectMake(0.0, 7.0, 70.0, 30.0); 
[profilebutton setImage:profileImg forState:UIControlStateNormal]; 
[profilebutton addTarget:self action:@selector(profileBtnClicked) forControlEvents:UIControlEventTouchUpInside]; 
[leftContainer addSubview:profilebutton]; 

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:leftContainer]; 
self.navigationItem.leftBarButtonItem = item; 

[leftContainer release]; 
[item release]; 
0

要向上調整文本,我不得不使用這樣的:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1.5) forBarMetrics:UIBarMetricsDefault];