Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background color of UIButton when Highlighted

Hi I want to change the UIButton background color which user Tap on the button.

I am showing background color using gradient, when user tap I need to change the gradient color.

[btn setTitle: @"Next" forState:UIControlStateNormal]; CAGradientLayer *gradient = [CAGradientLayer layer];             gradient.frame = btn.bounds; gradient.cornerRadius = 10.0f; locations = [[NSArray alloc] initWithObjects: 0.0f, 0.0f, 0.0f,0.0f, nil];  [gradient setLocations:locations]; colorNext = [[NSArray alloc] initWithObjects:…., nil];  gradient.colors = colorNext; [locations release]; [btn.layer insertSublayer:gradient atIndex:0]; btn.titleLabel.textColor = [UIColor blackColor]; 
like image 736
iPhoneDev Avatar asked Oct 26 '10 10:10

iPhoneDev


People also ask

How do I change the background color in UIButton?

To change the background color of a button in iOS application we need to access the property ' backgroundColor' of the UIButton. We can do this in two ways, programmatically and using the storyboard.

How do I change the button title color in Swift?

Is-it possible to change the color of the Title for the button ? Usually, use setTitleColor(_:for:) because buttons can have different UIControlState : . normal , .

How do you highlight a button in Swift?

In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.


1 Answers

Changing the internal layer hierarchy of a UIButton is not recommended since it may break in a future update of the iOS. Also, it may cause Apple to reject your application. A better solution would be to create a button subclass. Here is an example of how to do it:

@interface MyButton : UIButton @end  @implementation MyButton  - (id)initWithFrame:(CGRect)frame {     if (self = [super initWithFrame:frame])     {         [self addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:NULL];     }      return self; }  - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {     [self setNeedsDisplay]; }  - (void)drawRect:(CGRect)rect {     [super drawRect:rect];      if (self.highlighted == YES)     {         CGContextRef ctx = UIGraphicsGetCurrentContext();          CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();          const CGFloat *topGradientColorComponents = CGColorGetComponents([UIColor whiteColor].CGColor);         const CGFloat *bottomGradientColorComponents = CGColorGetComponents([UIColor blackColor].CGColor);          CGFloat colors[] =         {             topGradientColorComponents[0], topGradientColorComponents[1], topGradientColorComponents[2], topGradientColorComponents[3],             bottomGradientColorComponents[0], bottomGradientColorComponents[1], bottomGradientColorComponents[2], bottomGradientColorComponents[3]         };         CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));         CGColorSpaceRelease(rgb);          CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, 0), CGPointMake(0, self.bounds.size.height), 0);         CGGradientRelease(gradient);     }     else     {         // Do custom drawing for normal state     } }  - (void)dealloc {     [self removeObserver:self forKeyPath:@"highlighted"];      [super dealloc]; }  @end 

You may need to modify it a bit to get it to do what you want but I think you get the basic idea.

like image 123
loomer Avatar answered Sep 23 '22 01:09

loomer