ios - GradientLayer के साथ UIButton छवि को अंधेरे और ढाल गहरा
calayer cagradientlayer (3)
मेरे पास यहां एक UIButton है जहां मुझे छवि के नीचे पृष्ठभूमि (पारदर्शी पृष्ठभूमि वाला प्रतीक) के रूप में एक ढाल होना है, लेकिन मैं दो अलग-अलग समस्याओं का सामना कर रहा हूं
सबसे पहले CAGradientLayer छवि के शीर्ष पर ओवरले लगता है, भले ही मैं इसे जोड़ने का प्रयास कैसे करूँ, छवि को पूरी तरह से अस्पष्ट करना
दूसरा, ढाल के रूप में बहुत ही अंधेरे लगते हैं, जैसे कि बटन को अक्षम किया गया था, जो कि ऐसा नहीं है।
यहां मेरा कोड है:
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];
तो मैं एक [बटन bringSubviewToFront: बटन.imageView] ढाल जोड़ने के बाद इस के आसपास पाने में कामयाब रहा। ऐसा लगता है कि कोई भी चीज नहीं जो मैं करूँगा, वह छवि को देखने के शीर्ष पर जोड़ देगा, इसलिए मुझे इसके बाद के मोर्चे पर आगे बढ़ना होगा।
वैकल्पिक रूप से, आप छवि दृश्य की परत के नीचे ढाल परत डालें
CAGradientLayer* gradient = [CAGradientLayer layer];
// ...
[button.layer insertSublayer:gradient below:button.imageView.layer];
स्विफ्ट 3
आप छवि को ढाल के ऊपर स्थानांतरित कर सकते हैं:
contactButton.imageView?.layer.zPosition = 1
या छवि के तहत ढाल डालें:
button.layer.insertSublayer(gradientLayer, below: button.imageView?.layer)