2011-11-12 12 views
31

Ho un UIButton qui dove mi piacerebbe avere un gradiente come sfondo sotto l'immagine (simbolo con sfondo trasparente), ma sto affrontando due problemi diversi.UIButton con GradientLayer oscura l'immagine e oscura il gradiente

Il primo CAGradientLayer sembra sovrapporsi all'immagine, non importa quanto cerco di aggiungerlo, oscurando completamente l'immagine.

In secondo luogo, il gradiente in sé sembra essere molto oscurato, come il pulsante disabilitato, che non lo è.

Ecco il mio codice:

self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 35, 28, 28)]; 
[backButton addTarget:self 
       action:@selector(goBack) 
    forControlEvents:UIControlEventTouchUpInside]; 
[backButton setBackgroundColor:[UIColor clearColor]]; 
CAGradientLayer *buttonGradient = [CAGradientLayer layer]; 
buttonGradient.frame = backButton.bounds; 
buttonGradient.colors = [NSArray arrayWithObjects: 
         (id)[[UIColor colorWithRed:.0 
               green:.166 
               blue:.255 
               alpha:1] CGColor], 
         (id)[[UIColor colorWithRed:.0 
               green:.113 
               blue:.255 
               alpha:1] CGColor], 
         nil]; 
[buttonGradient setCornerRadius:backButton.frame.size.width/2]; 
[backButton.layer insertSublayer:buttonGradient 
         atIndex:0]; 
[backButton setImage:[UIImage imageNamed:@"backarrow.png"] 
      forState:UIControlStateNormal]; 
[backButton setEnabled:NO]; 
[topbarView addSubview:backButton]; 

risposta

61

così sono riuscito a aggirare il problema facendo un [tasto bringSubviewToFront: button.imageView] dopo aver aggiunto il gradiente. Sembra che non importa quello che faccio, il nuovo livello si aggiungerà a imageView, quindi ho bisogno di spingerlo in avanti.

+0

bella soluzione, grazie –

+0

Perfetto, grazie – avdyushin

21

In alternativa, è possibile inserire il livello di sfumatura sotto strati della visualizzazione dell'immagine

CAGradientLayer* gradient = [CAGradientLayer layer]; 
// ... 
[button.layer insertSublayer:gradient below:button.imageView.layer]; 
+0

È letteralmente mi hai appena salvato la vita! ;) L'immagine non è stata mostrata dopo aver aggiunto il livello con sfumatura perché il livello era ovviamente - sopra il livello dell'immagine. Grazie – virusss8

1

SWIFT 3

qui va un esempio: (Basato su Neil risposta)

let layer = CAGradientLayer() 
    layer.frame = CGRect(x: 0, y: 0, width: myButtonOutlet.layer.frame.size.width, height: myButtonOutlet..layer.frame.size.height) 
    layer.colors = [UIColor.white.cgColor, pixelColorReceta.cgColor] 
    layer.masksToBounds = true 
    layer.cornerRadius = myButtonOutlet.layer.cornerRadius 
    myButtonOutlet.layer.insertSublayer(layer, below: myButtonOutlet..imageView?.layer) 

Felice codifica :)

0

Swift 3

È possibile spostare l'immagine qui sopra il gradiente:

contactButton.imageView?.layer.zPosition = 1 

o inserire il gradiente sotto l'immagine:

button.layer.insertSublayer(gradientLayer, below: button.imageView?.layer)