2012-06-23 15 views
5

Ich erstelle eine Schaltfläche, und ich möchte die Farbe ändern, wenn der Benutzer geklickt hat. Ich habe die Option "setimagebackground ... forstate ..." gesehen, aber es gibt keine andere Möglichkeit, die Hintergrundfarbe zu ändern. Ich habe die Werte der y-Taste in viewdidload geändert und wenn ich "settinct color: [uicolor redcolor]" verwende, ist nichts passiert. Wie kann ich es beheben? Hier ist mein Code:Schaltfläche ändern Hintergrundfarbe beim Klicken

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

Dank!

+0

siehe diese Frage: http: //stackoverflow.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – self

+0

gibt es keine Möglichkeit, es ohne zu tun Bild? – cnaaniomer

+0

Es gibt eine Antwort in diesem Post. funktioniert gut http://stackoverflow.com/questions/9737503/changing-uibutton-background-color – tiagouy

Antwort

4

Bild:

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

Ohne Bild:

[button1 setBackgroundColor:[UIColor redColor] ]; 

Mit diesem können Sie abgerundete Ränder bekommen:

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

Für den Staat Unterklasse ausgewählt UIButton auf diese Weise:

In .h-Datei:

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

} 

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

@end 

in .m-Datei:

#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 

Dann in Ihrem Viewcontroller (Denken Sie daran, Ihre benutzerdefinierte Klasse enthalten) können Sie die Farbe auf diese Weise einstellen:

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

aber ich möchte es die Farbe ändern, wenn angeklickt wird. Der Code, den Sie eingeben, ändert die Farbe der Schaltfläche (und die gleiche, wenn Sie auf klicken) – cnaaniomer

+0

ich aktualisierte meine Antwort – self

Verwandte Themen