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:
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!
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:
In .h file:
@interface MyButton : UIButton
{
@private
NSMutableDictionary *backgroundStates;
@public
}
- (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state;
- (UIColor*) backgroundColorForState:(UIControlState) _state;
@end
In .m file:
#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:
[button1 setBackgroundColor:[UIColor colorWithRed:0.8 green:0.7 blue:0.6 alpha:1.0] forState:UIControlStateHighlighted];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With