2012-06-23 24 views
5

Creo un botón y quiero cambiar su color cuando el usuario hace clic. Vi la opción "setimagebackground ... forstate ...", pero no hay otra opción para cambiar el color de fondo. Tengo los valores de cambio del botón y en viewdidload y cuando uso "settint color: [uicolor redcolor]" no pasó nada. ¿Cómo puedo solucionarlo? Aquí está mi código:cambiar el color de fondo del botón al hacer clic en

button1.layer.borderColor = [[UIColor purpleColor]CGColor]; 
    button1.layer.cornerRadius = 8.0f; 
    button1.layer.masksToBounds = YES; 
    button1.layer.borderWidth = 1.0f; 
    [button1.tintColor = [UIColor redColor]CGColor]; 
    [button1 setTintColor:[UIColor blueColor]]; 
    [button1 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal]; 
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; 
    [button1 setTintColor:[UIColor redColor]]; 

gracias!

+0

vea esta pregunta: http: //stackoverflow.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – self

+0

no hay opción de hacerlo sin una ¿imagen? – cnaaniomer

+0

Hay una respuesta en esta publicación. funciona bien http://stackoverflow.com/questions/9737503/changing-uibutton-background-color – tiagouy

Respuesta

4

Con Image:

[button1 setBackgroundImage:[UIImage imageNamed:@"redButton.png"]]; 

Sin Image:

[button1 setBackgroundColor:[UIColor redColor] ]; 

Con esto se puede conseguir bordes redondeados:

 CALayer * layer = [button1 layer];  
    [layer setCornerRadius:8.0f]; 
    [layer setMasksToBounds:YES]; 
    [layer setBorderWidth:1.0f]; 
    [layer setBorderColor:[[UIColor whiteColor] CGColor]]; 
    button1.backgroundColor = [UIColor redColor]; 

Para la subclase estado seleccionado UIButton de esta manera:

En el archivo .h:

@interface MyButton : UIButton 
{ 
@private 
    NSMutableDictionary *backgroundStates; 
@public 

} 

- (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state; 
- (UIColor*) backgroundColorForState:(UIControlState) _state; 

@end 

en el archivo .m:

#import "MyButton.h" 


@implementation MyButton 

- (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state { 
    if (backgroundStates == nil) 
     backgroundStates = [[NSMutableDictionary alloc] init]; 

    [backgroundStates setObject:_backgroundColor forKey:[NSNumber numberWithInt:_state]]; 

    if (self.backgroundColor == nil) 
     [self setBackgroundColor:_backgroundColor]; 
} 

- (UIColor*) backgroundColorForState:(UIControlState) _state { 
    return [backgroundStates objectForKey:[NSNumber numberWithInt:_state]]; 
} 

#pragma mark - 
#pragma mark Touches 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 

    UIColor *selectedColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateHighlighted]]; 
    if (selectedColor) { 
     CATransition *animation = [CATransition animation]; 
     [animation setType:kCATransitionFade]; 
     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
     [self.layer addAnimation:animation forKey:@"EaseOut"]; 
     self.backgroundColor = selectedColor; 
    } 
} 

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesCancelled:touches withEvent:event]; 

    UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]]; 
    if (normalColor) { 
     CATransition *animation = [CATransition animation]; 
     [animation setType:kCATransitionFade]; 
     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
     [self.layer addAnimation:animation forKey:@"EaseOut"]; 
     self.backgroundColor = normalColor; 
    } 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesEnded:touches withEvent:event]; 

    UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]]; 
    if (normalColor) { 
     CATransition *animation = [CATransition animation]; 
     [animation setType:kCATransitionFade]; 
     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
     [self.layer addAnimation:animation forKey:@"EaseOut"]; 
     self.backgroundColor = normalColor; 
    } 
} 

- (void) dealloc { 
    [backgroundStates release]; 
    [super dealloc]; 
} 

@end 

Luego, en su ViewController (Recuerde incluir su clase personalizada) se puede establecer el color de esta manera:

[button1 setBackgroundColor:[UIColor colorWithRed:0.8 green:0.7 blue:0.6 alpha:1.0] forState:UIControlStateHighlighted]; 
+0

pero quiero que cambie su color cuando se hace clic en. el código que le das cambia el color del botón (y es el mismo cuando haces clic) – cnaaniomer

+0

actualicé mi respuesta – self

Cuestiones relacionadas