2012-06-23 25 views
5

Creo un pulsante e voglio cambiare il suo colore quando l'utente ha fatto clic. Ho visto l'opzione "setimagebackground ... forstate ...", ma non c'è altra opzione per cambiare il colore di sfondo. Ho cambiato i valori del pulsante y in viewdidload e quando uso "settint color: [uicolor redcolor]" non è successo nulla. Come posso risolvere il problema ?? Ecco il mio codice:cambia colore di sfondo pulsante quando si fa clic su

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]]; 

grazie!

+0

vedi questa domanda: http: //stackoverflow.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – self

+0

non c'è alcuna opzione per farlo senza un Immagine? – cnaaniomer

+0

C'è una risposta in questo post. funziona bene http://stackoverflow.com/questions/9737503/changing-uibutton-background-color – tiagouy

risposta

4

Con Image:

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

senza immagine:

[button1 setBackgroundColor:[UIColor redColor] ]; 

Con questo si può ottenere bordi arrotondati:

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

Per la sottoclasse stato selezionato UIButton in questo modo:

Nel file .h:

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

} 

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

@end 

Nel file di .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 

Poi, nel tuo ViewController (Ricordati di includere la classe personalizzata) è possibile impostare il colore in questo modo:

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

ma voglio che cambi il suo colore quando si fa clic su. il codice che dai cambia il colore del bottone (e lo stesso quando clicchi) – cnaaniomer

+0

ho aggiornato la mia risposta – self

Problemi correlati