2012-06-04 28 views
7

Estoy personalizando el botón Atrás en la barra de navegación mediante el proxy de aparición, también estableciendo atributos de imagen y texto. Todo funciona bien pero el texto en el botón Atrás no está alineado en el centro.Barra de navegación Botón Atrás, texto no centrado aliginado

Aquí está mi código.

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; 

Si configuro el tamaño de fuente "0.0", el texto se vuelve demasiado pequeño y sigue sin estar centrado.

Gracias de antemano por cualquier ayuda.

+0

¿Se refiere a la alineación vertical u horizontal de la alineación? –

+0

Creo que intente esto puede ayudar no sur pero hago esto en textview y también en lable pero no en BarButton así que solo intentaré y luego publicaré la respuesta [[UIBarButtonItem aparición] setTextAlignment: UITextAlignmentCenter]; :) –

+0

ParasJoshi: no funcionó. – fibnochi

Respuesta

17

Puede usar el método setTitlePositionAdjustment: forBarMetrics: para establecer el desplazamiento del título según sea necesario. Por ejemplo:

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

Funcionó para mí (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

Para ajustar el texto hasta que tuve que usar esto:

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