2012-06-23 19 views
5

Tworzę przycisk i chcę zmienić jego kolor po kliknięciu przez użytkownika. Widziałem opcję "setimagebackground ... forstate ...", ale nie ma innej opcji zmiany koloru tła. Mam zmiany wartości przycisku y w viewdidload i kiedy używam "settint color: [uicolor redcolor]" nic się nie stało. Jak mogę to naprawić? Oto mój kod:zmienia kolor tła przycisku po kliknięciu

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

dziękuję!

+0

zobacz to pytanie: http: //stackoverflow.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – self

+0

nie ma opcji, aby zrobić to bez obraz? – cnaaniomer

+0

Istnieje odpowiedź w tym poście. działa dobrze http://stackoverflow.com/questions/9737503/changing-uibutton-background-color – tiagouy

Odpowiedz

4

Z obrazu:

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

Bez obrazu:

[button1 setBackgroundColor:[UIColor redColor] ]; 

z tym można uzyskać zaokrąglone granice:

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

Dla wybranego podklasy państwowej UIButton w ten sposób:

W pliku .h:

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

} 

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

@end 

W .m pliku:

#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 

Następnie w ViewController (Pamiętaj, aby podać swoją klasę niestandardową) można ustawić kolor w ten sposób:

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

, ale chcę, aby zmienił kolor po kliknięciu. kod, który podasz, zmienia kolor przycisku (i jest taki sam po kliknięciu) – cnaaniomer

+0

zaktualizowałem moją odpowiedź – self

Powiązane problemy