2010-11-22 9 views

Respuesta

34

iOS 8 (UIAlertController)

Es super fácil de hacer si usted está utilizando una UIAlertController. Simplemente cambie el color del tinte en la vista del UIAlertController.

[alertController.view setTintColor:[UIColor red]; 

iOS 7 (UIActionSheet)

puedo cambiar el color del texto con éxito mediante el uso de este método simple.

- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet { 
    UIColor *tintColor = [UIColor redColor]; 

    NSArray *actionSheetButtons = actionSheet.subviews; 
    for (int i = 0; [actionSheetButtons count] > i; i++) { 
     UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i]; 
     if([view isKindOfClass:[UIButton class]]){ 
      UIButton *btn = (UIButton*)view; 
      [btn setTitleColor:tintColor forState:UIControlStateNormal]; 

     } 
    } 
} 

Asegúrese de ejecutar este DESPUÉS se llama

[actionSheet showInView]; 

Si llama antes [showInView], todos los botones, pero será de color del botón de cancelación. ¡Espero que esto ayude a alguien!

+0

Esta debería ser la respuesta seleccionada. Bien hecho. – dclowd9901

+0

@ dclowd9901 ¡Gracias! ;) – Rymnel

+2

Gracias - funciona bien en iOS7. – kmorris

1

Desafortunadamente, sin utilizar las API no documentadas, no hay forma oficial de cambiar el color del botón en una Hoja de UIAction. Puede personalizar esto si subclase el control UIActionSheet.

ver este ejemplo: http://blog.corywiles.com/customizing-uiactionsheet-buttons

+0

¿Alguna vez se ha hecho esto? Cualquier código de muestra será realmente útil. – Abhinav

+0

Ver el enlace que publiqué arriba. –

+0

¡Muchas gracias! – Abhinav

7

He creado UICustomActionSheet clase hija, que permite personalizar las fuentes, colores e imágenes de botones dentro de UIActionSheet. Está absolutamente seguridad para appstore, se puede encontrar el código de esta clase en el siguiente enlace:

https://github.com/gloomcore/UICustomActionSheet

disfrutar de ella!

+0

¿Está seguro de que está bien para la tienda de aplicaciones? De todos modos el resultado es genial! –

+0

Sí, estoy seguro, lo he usado en aplicaciones para Appstore. Esta clase no usa métodos privados, por lo que está claro. – Gloomcore

+0

¡Eso es increíble! ¡Gracias por publicar! – yonix

1

Podemos usar imágenes de fondo para hacerlo. Creo que es la manera más fácil.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actionsheet" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
     [actionSheet addButtonWithTitle:@"Button 1"]; //Blue color 
     [actionSheet addButtonWithTitle:@"Button 2"]; 
     [actionSheet addButtonWithTitle:@"Cancel"]; 
     [actionSheet addButtonWithTitle:nil]; 
     [actionSheet setCancelButtonIndex:2]; 
     [actionSheet setDestructiveButtonIndex:1]; 
     [actionSheet showInView:self.view]; 
     UIButton *button = [[actionSheet subviews] objectAtIndex:1]; 
     UIImage *img = [button backgroundImageForState:UIControlStateHighlighted];//[UIImage imageNamed:@"alert_button.png"]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
0

Se puede lograr fácilmente mediante el uso siguiente código

de Apple UIActionSheetDelegate documentación del protocolo

https://developer.apple.com/library/ios/documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    for (UIView *_currentView in actionSheet.subviews) 
    { 
     if ([_currentView isKindOfClass:[UIButton class]]) 
     {  
      UIButton *button = (UIButton *)_currentView; 
      [button setTitleColor:YOUR_COLOR forState:UIControlStateNormal]; 
     } 
    } 
} 
Cuestiones relacionadas