0

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.

giorashc
  • 13,691
  • 3
  • 35
  • 71
IsaacEllis
  • 33
  • 3

3 Answers3

1

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.
Community
  • 1
  • 1
Oleksandr
  • 419
  • 3
  • 14
0

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.};
Cyrille
  • 25,014
  • 12
  • 67
  • 90
0

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • [http://mobile.tutsplus.com/tutorials/iphone/custom-uibutton_iphone/] i downloaded the source and it work i can't see the difference between the my project and that project – IsaacEllis Oct 28 '12 at 09:46