IsaacEllis
IsaacEllis

Reputation: 33

Problems with CAGradientLayer

Im using the code

NSArray *buttons = [NSArray arrayWithObjects: self.rollBtn,nil];        
for(UIButton *btn in buttons)
{       
    btn.layer.shadowRadius = 3.0;
    btn.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
    btn.layer.shadowOpacity = 0.5;
    btn.layer.shadowColor = [UIColor blackColor].CGColor;
    CAGradientLayer *btnGradient = [CAGradientLayer layer];
    btnGradient.frame = btn.bounds;
    btnGradient.colors = [NSArray arrayWithObjects:
                          (id)[[UIColor colorWithRed:102.0f / 255.0f green:102.0f / 255.0f blue:102.0f / 255.0f alpha:1.0f] CGColor],
                          (id)[[UIColor colorWithRed:51.0f / 255.0f green:51.0f / 255.0f blue:51.0f / 255.0f alpha:1.0f] CGColor],
                          nil];

    [btn.layer insertSublayer:btnGradient atIndex:0];            
}

But all i get is a button with a drop shadow. I've linked the quartz core library, ive imported it, i've linked the buttons, ive tried using different types of buttons; i'm Stumped. Any Ideas whats going wrong? Thanks in advance.

Upvotes: 0

Views: 510

Answers (3)

Oleksandr
Oleksandr

Reputation: 449

I faced same problem. It seems you are building for iOS6 as I do. I found that Autolayout for storyboards, introduced in iOS6, is causing such problems. Just disable it if you don't use this feature:

  1. Open storyboard in Xcode
  2. Open properties of storyboard (right pane with properties)
  3. Uncheck "Use Autolayout" in "Interface Builder Document" section.

Upvotes: 1

sergio
sergio

Reputation: 69047

I think you could try to remove any previously existing CAGradientLayer in your button:

for(CALayer* layer in btn.layer.sublayers)
    if ([layer isKindOfClass:[CAGradientLayer class]])
        [layer removeFromSuperlayer];

and then adding your own.

Upvotes: 0

Cyrille
Cyrille

Reputation: 25144

Did you try setting the locations and the start/end points for your gradient ?

btnGradient.locations  = @[@(0.), @(1.)];
btnGradient.startPoint = (CGPoint){0., 0.};
btnGradient.endPoint   = (CGPoint){0., 1.};

Upvotes: 0

Related Questions