7

For an app I had to create a UIButton filled with a gradient and a background image. Everything worked fine until I upgraded the OS from iOS 5.1 to the recently released iOS 6.

Here are two screenshots from the simulator :

screenshot with iOS 5.1

Screnshot with iO 6

Well, the first screenshot shows what I need (and did), you can see a brown background and the grey radient.

Below is the screenshot with the same buttons but with iOS 6 running. As you can see the gradient has vanished and a strange white strip has appeared at the bottom of the UIButton.

I've looked if this is a bug or something but I've found nothing, maybe someone here has faced the same problem? Here is my code for gradient

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = btnCountry.bounds;
    UIColor *colorTop = [UIColor colorWithRed:.992f green:.992f blue:.992f alpha:1];
    UIColor *colorBot = [UIColor colorWithRed:.788f green:.769f blue:.745f alpha:1];
    gradient.colors = [NSArray arrayWithObjects:(id)[colorTop CGColor], (id)[colorBot CGColor], nil];
    gradient.borderColor = [UIColor colorWithRed:.545f green:.506f blue:.459f alpha:1].CGColor;
    gradient.borderWidth = 1;

    gradient.masksToBounds = YES;
    gradient.cornerRadius = 11;
    [[btnCountry layer] insertSublayer:gradient atIndex:0];
IronManGill
  • 7,222
  • 2
  • 31
  • 52
Edelweiss
  • 621
  • 1
  • 9
  • 24
  • 1
    I finally fin a way to restore the "normal" look of my buttons by removing a subView with this code : `for(UIView* subView in btnCountry.subviews) if([subView isKindOfClass:NSClassFromString(@"UIGroupTableViewCellBackground")]) [subView removeFromSuperview];` but now, when I tap the buttons it cause an error EXC_BAD_ACC, nobody has a clue or something? – Edelweiss Sep 25 '12 at 07:46
  • this [question](http://stackoverflow.com/questions/15459796/what-is-a-uigrouptableviewcellbackground-in-ios6) is related – abbood Mar 17 '13 at 10:37

3 Answers3

11

This is a really strange problem in ios6 ,I faced the same problem when setting gradient like you typically would:

[myButton.layer insertSublayer:gradient atIndex:0];

so I tried changing the bottom line to this which worked perfectly fine in iOS 6 and also in lower versions of IOS

[myButton.layer insertSublayer:gradient below:myButton.titleLabel.layer];

hope this will help

Samyukt Shah
  • 1,337
  • 12
  • 13
7

As you can see in my comment the problem came from the class UIGroupTableViewCellBackground, I just hide it. I think this is not a "clean" solution, if you have a better one İ'll be glad to hear about it :-)

Here is the code :

for(UIView* subView in btnCountry.subviews)
    if([subView isKindOfClass:NSClassFromString(@"UIGroupTableViewCellBackground")])
        [subView setHidden:YES];
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
Edelweiss
  • 621
  • 1
  • 9
  • 24
2

Inserting the layer at index position 1 works for me.

Probably the best solution is to check the iOS version and depending on that, insert at index 0 or 1.

Salazar
  • 401
  • 1
  • 3
  • 8