Я использую CAGradientLayer для стилизации моих кнопок uibuttons. Обычно в ячейке три кнопки. Когда я сначала устанавливаю свойства CAGradientLayer, он работает нормально (я получаю градиент).

Однако, когда я применяю CAGradientLayer ко второму, он появляется на втором, но исчезает с первого. И если я сделаю это на третьем, то он появится на третьем и исчезнет с первых двух.

Должен ли я выпускать CAGradientLayer между установками UIButton?

Вот пример кода:

 //like button
 self.likeButton=[[UIButton alloc] initWithFrame:CGRectMake(10, 430, 80, 26)];
 [self.likeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
 [self.likeButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
 self.likeButton.tag=indexPath.row;
 [self.likeButton addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
 [self.likeButton setBackgroundColor:[UIColor blackColor]];
 [self.likeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 [self.likeButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];


 CAGradientLayer *btnGradient=[CAGradientLayer layer];
 btnGradient.frame=self.likeButton.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:41.0f / 255.0f green:41.0f / 255.0f blue:41.0f / 255.0f alpha:1.0f]CGColor], nil];
 [self.likeButton.layer insertSublayer:btnGradient atIndex:0];

 CALayer *btnLayer=self.likeButton.layer;
 [btnLayer setMasksToBounds:YES];
 [btnLayer setCornerRadius:5.0f];
 [btnLayer setBorderWidth:1.0f];
 [btnLayer setBorderColor:[[UIColor darkGrayColor]CGColor]];

 [cell addSubview:self.likeButton];


 //comment button
 UIButton *commentButton=[[UIButton alloc]initWithFrame:CGRectMake(110, 430, 80, 26)];
 [commentButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
 [commentButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
 [commentButton setBackgroundColor:[UIColor blackColor]];
 [commentButton setTitle:@"Comment" forState:UIControlStateNormal];
 [commentButton addTarget:self action:@selector(commentButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
 [commentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 [commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];


 CAGradientLayer *commentBGLayer=[CAGradientLayer layer];
 commentBGLayer.frame=commentButton.bounds;
 commentBGLayer.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:41.0f / 255.0f green:41.0f / 255.0f blue:41.0f / 255.0f alpha:1.0f]CGColor], nil];
 [commentButton.layer insertSublayer:btnGradient atIndex:0];


 CALayer *commentBLayer=commentButton.layer;
 [commentBLayer setMasksToBounds:YES];
 [commentBLayer setCornerRadius:5.0f];
 [commentBLayer setBorderWidth:1.0f];
 [commentBLayer setBorderColor:[[UIColor darkGrayColor]CGColor]];
 [cell addSubview:commentButton];
0
Live2Enjoy7 16 Фев 2013 в 00:47

1 ответ

Лучший ответ

У вас ошибка копирования и вставки. Вы создаете два разных слоя градиента, но добавляете один и тот же к каждой кнопке - эту строку:

[commentButton.layer insertSublayer:btnGradient atIndex:0];

Должен прочесть:

[commentButton.layer insertSublayer:commentBGLayer atIndex:0];
0
jrturton 16 Фев 2013 в 00:55