2012-06-23 147 views
5

我創建一個按鈕,並且當用戶單擊時我想更改它的顏色。我看到選項「setimagebackground ... forstate ...」,但沒有其他選項可以更改背景顏色。我在viewdidload中改變了y按鈕的值,當我使用「settint color:[uicolor redcolor]」時,什麼都沒有發生。 我該如何解決? 這裏是我的代碼:單擊時更改按鈕背景顏色

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

謝謝!

+0

看到這個問題:http://stackoverflow.com/questions/948106/how-can-i-change-the-default-color-of-the-button-on-iphone – self

+0

有沒有選擇做沒有一個圖片? – cnaaniomer

+0

在這篇文章中有一個答案。效果不錯 http://stackoverflow.com/questions/9737503/changing-uibutton-background-color – tiagouy

回答

4

隨着圖片:

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

無圖像:

[button1 setBackgroundColor:[UIColor redColor] ]; 

有了這個,你可以得到圓角邊框:

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

對於以這種方式選擇的狀態子的UIButton:

在.h文件中:

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

} 

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

@end 

在.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 

那麼你的視圖控制器(請記住,包括自定義類),你可以設置這樣的顏色:

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

但我希望它在點擊時改變它的顏色。你給的代碼改變了按鈕的顏色(和點擊時相同) – cnaaniomer

+0

我更新了我的答案 – self

相關問題