Change UIButton gradient color - ios

I use the following method to add some gradient effect to my IOS UIButton. I call it in viewController's viewDidLoad, works great, so far so good.
Now, I'd like to change the color of the button according to some user interaction. What I do is simply calling this method by different colors (fromColor, toColor) as input. The problem, the look of my button does not change. I tried to call setNeedsDisplay, but did not help.
Would you be so kind to help me? What I miss? There is should be some problem with layers, should be rested or something like that, but I could not find its proper way.
Update: By using setSubLayers instead of insertSubLayer color changes, but button title disappears.
+(void)setCustomButtonStyle:(UIButton *)button cornerRadius:(float) cornerRadius fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor normalTitleColor:(UIColor *)normalTitleColor highlightedTitleColor:(UIColor *)highlightedTitleColor borderWidth:(float) borderWidth borderColor:(UIColor*)borderColor
{
[button setTitleColor:normalTitleColor forState:UIControlStateNormal];
[button setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = button.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[fromColor CGColor],
(id)[toColor CGColor],
nil];
btnGradient.borderWidth = borderWidth;
btnGradient.borderColor = [borderColor CGColor];
[button.layer insertSublayer:btnGradient atIndex:0];
CALayer *btnLayer = [button layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:cornerRadius];
}

The problem you are having here is that 1. you are creating a new layer each time you are changing the colour and 2. you are then placing that new layer underneath everything else so you can't see it anyway. (Think of putting a playing card at the bottom of the deck).
What you need to do is keep hold of the gradient layer and then update then instead of creating a new one.
You are probably best doing this in a category on UIButton.
Something like... UIButton+GradientBackground or something.
In the .h file give it a single function...
// Use CGFloat not float. Also user NSInteger not int.
// Always.
// Also, I have used modern Obj-C syntax. Use this too.
- (void)setCornerRadius:(CGFloat)cornerRadius
fromColor:(UIColor *)fromColor
toColor:(UIColor *)toColor
normalTitleColor:(UIColor *)normalTitleColor
highlightedTitleColor:(UIColor *)highlightedTitleColor
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor *)borderColor;
Then in the .m file you need to also give it a property...
#property (nonatomic, strong) CAGradientLayer *gradientLayer;
Now in the function...
- (void)setCornerRadius:(CGFloat)cornerRadius
fromColor:(UIColor *)fromColor
toColor:(UIColor *)toColor
normalTitleColor:(UIColor *)normalTitleColor
highlightedTitleColor:(UIColor *)highlightedTitleColor
borderWidth:(CGFloat)borderWidth
borderColor:(UIColor *)borderColor
{
[self setTitleColor:normalTitleColor forState:UIControlStateNormal];
[self setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
if (!self.grandientLayer) {
// if it doesn't exist then create it and add it (only once)
self.gradientLayer = [CAGradientLayer layer];
[self.layer insertSublayer:self.gradientLayer atIndex:0];
}
self.gradientLayer.frame = self.bounds;
self.gradientLayer.colors = #[(id)fromColor.CGColor, (id)toColor.CGColor]; // use modern syntax
self.gradientLayer.borderWidth = borderWidth;
self.gradientLayer.borderColor = borderColor.CGColor;
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:cornerRadius];
}
Notice that I have removed the button from the function name as this function will essentially be part of that UIButton's functions so self is the button you are changing.
You would call it like this...
[someButton setCornerRadius:5 fromColor:[UIColor redColor] toColor... and so on];
By doing this you only create that gradient layer once and you keep hold of it so that you can update it the next time round.

For first time add gradient layer, next time replace added gradient layer
Added Category as #Fogmeister thinks this can be used with category only
#interface UIButton (gradient)
+(void)setCustomButtonStyle:(UIButton *)button cornerRadius:(float) cornerRadius fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor normalTitleColor:(UIColor *)normalTitleColor highlightedTitleColor:(UIColor *)highlightedTitleColor borderWidth:(float) borderWidth borderColor:(UIColor*)borderColor;
#end
#implementation UIButton (gradient)
+(void)setCustomButtonStyle:(UIButton *)button cornerRadius:(float) cornerRadius fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor normalTitleColor:(UIColor *)normalTitleColor highlightedTitleColor:(UIColor *)highlightedTitleColor borderWidth:(float) borderWidth borderColor:(UIColor*)borderColor
{
//Set Title color as you want
[button setTitleColor:normalTitleColor forState:UIControlStateNormal];
[button setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
//check gradientlayer exist
id layer = nil;
if (button.layer.sublayers.count > 0) {
layer = [button.layer.sublayers objectAtIndex:0];
}
//Find added gradient layer
CAGradientLayer *addedGradLayer = nil;
if (layer) {
if ([layer isKindOfClass:[CAGradientLayer class]]) {
addedGradLayer = (CAGradientLayer *)[button.layer.sublayers objectAtIndex:0];
}
}
//check gradient layer exists
if (!addedGradLayer) //first time
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = button.layer.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:
(id)fromColor.CGColor,
(id)toColor.CGColor,
nil];
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
gradientLayer.cornerRadius = button.layer.cornerRadius;
[button.layer insertSublayer:gradientLayer atIndex:0];
button.clipsToBounds = YES;
}
else //next time
{
CAGradientLayer *newGradientLayer = [CAGradientLayer layer];
newGradientLayer.frame = button.layer.bounds;
newGradientLayer.colors = [NSArray arrayWithObjects:
(id)fromColor.CGColor,
(id)toColor.CGColor,
nil];
newGradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:1.0f],
nil];
newGradientLayer.cornerRadius = button.layer.cornerRadius;
[button.layer replaceSublayer:addedGradLayer with:newGradientLayer];
}
}
#end

Related

code not running when remove full view and again add UIslider background color

The first time it works nice but second time when changes view constraint and again going to this function it crashes in below code.
anyone know please let me know thank you.
below code running first time perfect second time in the same view not running perfectly.
UIView *view = (UIView*)[slider.subviews objectAtIndex:0];
-(void)setGradientToSlider:(UISlider *)slider WithColors:(NSArray*)colorArray
{
UIView *view;
UIImageView *max_trackImageView;
view = (UIView*)[slider.subviews objectAtIndex:0];
max_trackImageView = (UIImageView*)[view.subviews objectAtIndex:0];
//setting gradient to max track image view.
CAGradientLayer* max_trackGradient = [CAGradientLayer layer];
CGRect rect=max_trackImageView.frame;
rect.origin.x=view.frame.origin.x;
max_trackGradient.frame=rect;
max_trackGradient.colors = colorArray;
[max_trackGradient setStartPoint:CGPointMake(0.0, 0.5)];
[max_trackGradient setEndPoint:CGPointMake(1.0, 0.5)];
[view.layer setCornerRadius:5.0];
[max_trackImageView.layer insertSublayer:max_trackGradient atIndex:0];
//Setting gradient to min track ImageView.
CAGradientLayer* min_trackGradient = [CAGradientLayer layer];
UIImageView *min_trackImageView;
min_trackImageView = (UIImageView*)[slider.subviews objectAtIndex:1];
rect=min_trackImageView.frame;
rect.size.width=max_trackImageView.frame.size.width;
rect.origin.y=0;
rect.origin.x=0;
min_trackGradient.frame=rect;
min_trackGradient.colors = colorArray;
[min_trackGradient setStartPoint:CGPointMake(0.0, 0.5)];
[min_trackGradient setEndPoint:CGPointMake(1.0, 0.5)];
[min_trackImageView.layer setCornerRadius:5.0];
[min_trackImageView.layer insertSublayer:min_trackGradient atIndex:0];
}

How to avoid infinite layoutSubviews

I have a UITableViewCell subclass which overrides layoutSubviews to apply a fade effect to a webview. For the most part it works fine, but sometimes it ends up in a state where layoutSubvies is called constantly with alternating values for the content height for the webview.
Is there some other place I could apply this gradient to avoid this issue?
CAGradientLayer* gradientLayer = nil;
int webViewHeight = 0;
- (void)layoutSubviews
{
NSLog(#"layoutSubviews %f", self.webView.scrollView.contentSize.height);
[super layoutSubviews];
if (self.webView.scrollView.contentSize.height == webViewHeight) {
// nothing has changed
return;
}
if (gradientLayer != nil) {
[gradientLayer removeFromSuperlayer];
gradientLayer = nil;
}
// if cell height is less than web view height we must fade
if (self.frame.size.height < self.webView.scrollView.contentSize.height) {
webViewHeight = self.webView.scrollView.contentSize.height;
gradientLayer = [CAGradientLayer layer];
NSObject* fadeColor = (NSObject*)[UIColor colorWithWhite:1.0 alpha:.9].CGColor;
NSObject* transparentColor = (NSObject*)[UIColor colorWithWhite:1.0 alpha:0.0].CGColor;
gradientLayer.colors = [NSArray arrayWithObjects:transparentColor, fadeColor, nil];
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:1.0], nil];
gradientLayer.bounds = self.bounds;
gradientLayer.anchorPoint = CGPointZero;
[self.layer addSublayer:gradientLayer];
}
}
my TWO cents for similar issue. Hope can help others.
After debugging a little, I noted ALSO adding CALayer will re-invoke layoutSubviews
So if You call:
self.layer.addSublayer(anotherLayer)
you wil be re-called in layoutSubviews.

How to remove CAGradientLayer from UIButton when it's highlighted

I've added gradient effect for my Button and added it as a sub layer to my Button.
Code is :
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.myButton.layer.bounds;
gradientLayer.colors = [NSArray arrayWithObjects: (id)[[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor,(id) colorTwo.CGColor,(id) colorThree.CGColor,(id) colorFour.CGColor, nil];
gradientLayer.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil];
self.myButton.layer.cornerRadius = 5;
gradientLayer.cornerRadius = self.myButton.layer.cornerRadius;
[self.myButton.layer addSublayer:gradientLayer];
Now when my button is highlighted (touchedDown method called), I want to set new gradient to myButton. Hence I need to remove the old gradient effect when myButton is clicked and add new gradient.
How to remove the old gradient effect ?
I tried
for (CALayer *layer in [self.loginScreenView.btnPowerBroking.layer.sublayers copy]){
if ([[layer name] isEqualToString:#"gradientLayer"]) {
NSLog(#"Gradient layer found.");
}
}
But I can't get it's gradient layer.
Thanks for your time. (:
You haven't set the name property to your gradientLayer object just set it and it will work.
gradientLayer.name = #"gradientLayer";
[self.myButton.layer addSublayer:gradientLayer];
In for loop's if statement remove the layer using this [layer removeFromSuperlayer];
Note : You are adding layer in myButton object in searching inside the btnPowerBroking check properly for that also, you need to search in button where you have added Layer.
Try this method...
[self.myButton.layer replaceSublayer:oldgradientLayer with:newgradientLayer];

Replace a CALayer with a CAGradientLayer doesn't work

I'm developing an iOS app with latest SDK.
I want to change a background layer dynamically:
#import <QuartzCore/QuartzCore.h>
#interface MyClass : UIView
{
#private
CALayer* _gradientBackground;
}
And some methods:
- (CALayer*)createLayerWithColor:(UIColor*)color
{
CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(NSLayerX, NSLayerY,
NSLayerWidth, NSLayerHeight);
layer.backgroundColor = [color CGColor];
layer.cornerRadius = NSCornerRadius;
return layer;
}
- (CAGradientLayer*)createLayerWithGradient:(UIColor*)startColor
endColor:(UIColor*)endColor
{
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(NSLayerX, NSLayerY,
NSLayerWidth, NSLayerHeight);
gradientLayer.colors =
[NSArray arrayWithObjects:(id)[startColor CGColor],
(id)[endColor CGColor], nil];
gradientLayer.cornerRadius = NSCornerRadius;
return gradientLayer;
}
- (void)changeBackgroundWithLayer:(CALayer*)newLayer
{
if (_gradientBackground != nil)
[_gradientBackground removeFromSuperlayer];
_gradientBackground = newLayer;
[self.layer insertSublayer:newLayer atIndex:0];
}
And I do this to change background layer:
[self changeBackgroundWithLayer:[self createLayerWithGradient:startColor endColor:endColor]];
And sometimes with this:
[self changeBackgroundWithLayer:[self createLayerWithColor:newColor]];
The way I do is:
First solid layer, next gradient layer and finally solid layer.
I have also tried with this code with no result:
- (void)changeBackgroundWithLayer:(CALayer*)newLayer
{
if (_gradientBackground != nil)
[self.layer replaceSublayer:_gradientBackground with:newLayer];
else
[self.layer insertSublayer:newLayer atIndex:0];
_gradientBackground = newLayer;
}
But it doesn't work.
Any advice?
Try this
- (void)changeBackgroundWithLayer:(CALayer*)newLayer
{
[_gradientBackground removeFromSuperlayer], _gradientBackground = nil;
[self.layer insertSublayer:newLayer atIndex:0];
_gradientBackground = newLayer;
}
This will remove the _gradientBackground and set it to nil in every case, which is perfectly acceptable in Objective-C. The newLayer is added to the layer hierarchy and the gradientBackground layer is updated.

Grandient background for a view on iOS

I'm pretty new to iOS developpement, and I'm trying to set my view background to a gradient.
I created a CAGradientLayer and try to set it. It goes throught building, but my app crashes as soon as it open, throwing a "EXC_BAD_ACCESS".
Here is my code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CAGradientLayer *bgLayer = [CAGradientLayer layer];
UIColor *grey = [UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1.0];
UIColor *black = [UIColor colorWithRed:(180/255.0) green:(180/255.0) blue:(180/255.0) alpha:1.0];
NSNumber *top = [NSNumber numberWithFloat:0.0];
NSNumber *bot = [NSNumber numberWithFloat:1.0];
bgLayer.colors = [NSArray arrayWithObjects:grey, black, nil];
bgLayer.locations = [NSArray arrayWithObjects:top, bot, nil];
bgLayer.frame = self.view.bounds;
[self.view.layer addSublayer:bgLayer];
}
I think I understood that it comes from a bad memory management, but don't what I did wrong. Thanks for your answers in advance.
Note what the docs say about the colors:
An array of CGColorRef objects defining the color of each gradient stop.

Resources