2012-04-06 69 views
23

Estoy tratando de cambiar el color de fuente del texto en mi botón de retroceso en mi UINavigationControllerBarcambiar la fuente del botón Atrás de UINavigationController

[[UIBarButtonItem appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 

me da este error: [_UIBarItemAppearance setTitleColor: forState:]: no reconocido selector enviado a instancia 0x69aeb70 '

¿Alguna ayuda? ¡Gracias!

Respuesta

13

Utilice esta vez, función por defecto disponible en iOS 5

UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];  

    [backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont, 
                nil] forState:UIControlStateNormal]; 
+0

No funciona en iOS7 +. La respuesta de Itgiawa está trabajando para eso. – Tulleb

49
NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; 
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor]; 
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor]; 
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset]; 
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

Parece que funciona!

+0

FUNCIONA PERFECTAMENTE !!! –

+2

Mejor solución para usar las opciones de apariencia. Trabajan en todo el mundo, o por clase, o por ejemplo. Cualquiera que venga aquí debería usar esta respuesta en su lugar. – Heckman

+0

Depende completamente de la situación. Estaba trabajando en un juego y necesitaba cambiar la fuente solo temporalmente. ;-) – phikes

15
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIFont fontWithName:kDefaultFont size:16.0f],UITextAttributeFont, 
                nil] forState:UIControlStateNormal]; 
+5

Para IOS7, reemplace UITextAttributeFont con NSFontAttributeName –

+0

Funcionando perfectamente en iOS8, también cambié UITextAttributeFont a NSFontAttributeName y funcionó también – Yahia

6

y la solución hermosa, iOS7 + (a causa de nombres de atributos):

NSShadow *shadow = [NSShadow new]; 
[shadow setShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f]]; 
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)]; 

[[UIBarButtonItem appearance] setTitleTextAttributes:@{ 
     NSFontAttributeName: [UIFont systemFontOfSize:24], 
     NSForegroundColorAttributeName: [UIColor colorWithWhite:0.2 alpha:1.0], 
     NSShadowAttributeName: shadow, 
} forState:UIControlStateNormal]; 
0

solución en Swift 4:

UIBarButtonItem.appearance().setTitleTextAttributes(
[ 
    NSAttributedStringKey.font: UIFont(name: "MyriadPro-SemiboldCond", size: 16)!, 
    NSAttributedStringKey.foregroundColor: UIColor.white 
], for: .normal) 

Añadir esto a Ap pDelegate y se aplicará a todos los botones en la aplicación.

Cuestiones relacionadas