xcode 单击时更改按钮背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11170116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:40:19  来源:igfitidea点击:

change button background color when clicked

objective-cxcodeuibutton

提问by cnaaniomer

I create a button and I want to change its color when the user clicked on. I saw the option "setimagebackground... forstate...", but there is no other option for changing the background color. I have change values of y button in viewdidload and when I use "settint color:[uicolor redcolor]" nothing happened. How can I fix it?? Here is my code:

我创建了一个按钮,我想在用户点击时改变它的颜色。我看到了“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]];

thanks!

谢谢!

回答by self

With Image:

带有图像:

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

Without Image:

没有图像:

[button1 setBackgroundColor:[UIColor redColor] ];

With this you can get rounded borders:

有了这个,你可以获得圆形边框:

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

For the selected state subclass UIButton in this way:

对于选中状态的子类 UIButton 以这种方式:

In .h file:

在 .h 文件中:

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

}

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

@end

In .m file:

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

Then in your ViewController(Remember to include your custom class) you can set the color in this way:

然后在您的 ViewController(记住要包含您的自定义类)中,您可以通过这种方式设置颜色:

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