2014-11-22 11 views
29

मेरे पास UIButton है जो मानक आईओएस कीबोर्ड वर्णमाला बटन के समान है।UIButton नीचे छाया

मुझे यकीन नहीं है कि आईओएस ने कैसे किया है, नीचे की परत के लिए केवल छाया कैसे बनाएं।

enter image description here

मैं नीचे दिए गए कोड का उपयोग करें, लेकिन मैं सब तरफ एक छाया देखते हैं, न सिर्फ नीचे:

CALayer *buttonLayer = [[CALayer alloc] init]; 
buttonLayer.shadowColor = [UIColor grayColor].CGColor; 
buttonLayer.shadowOffset = CGSizeMake(0.f,1.f); 
buttonLayer.masksToBounds = NO; 
buttonLayer.shadowOpacity = 1.f; 

क्या आप मुझे बता कैसे एक ही प्रभाव को प्राप्त करने कर सकते हैं। अग्रिम में धन्यवाद।

+0

जिन्हें आपने देखा है: http://stackoverflow.com/questions/9336187/ios-create-one-sideded-dropshadow – Yaser

+0

मुझे लगता है कि आप भी स्थापित करने के लिए 'shadowRadius' को 0. – Aaron

+0

इसके अलावा,' maskToBounds' चाहते डिफ़ॉल्ट रूप से 'NO' है। – Aaron

उत्तर

48

आप cornerRadius और छाया गुण मिश्रण कर सकते हैं। मैं iOS पर यह परीक्षण किया 8.

ऑब्जेक्टिव-सी:

[self.globeButton setImage:[UIImage imageNamed:@"Globe"] forState:UIControlStateNormal]; 
self.globeButton.backgroundColor = [UIColor colorWithRed:171 green:178 blue:186 alpha:1.0f]; 
// Shadow and Radius 
self.globeButton.layer.shadowColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.25f] CGColor]; 
self.globeButton.layer.shadowOffset = CGSizeMake(0, 2.0f); 
self.globeButton.layer.shadowOpacity = 1.0f; 
self.globeButton.layer.shadowRadius = 0.0f; 
self.globeButton.layer.masksToBounds = NO; 
self.globeButton.layer.cornerRadius = 4.0f; 

स्विफ्ट:

globeButton.setImage(UIImage(named: "Globe"), forState: .Normal) 
globeButton.backgroundColor = UIColor(red: 171, green: 178, blue: 186, alpha: 1.0) 
// Shadow and Radius 
globeButton.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).CGColor 
globeButton.layer.shadowOffset = CGSizeMake(0.0, 2.0) 
globeButton.layer.shadowOpacity = 1.0 
globeButton.layer.shadowRadius = 0.0 
globeButton.layer.masksToBounds = false 
globeButton.layer.cornerRadius = 4.0 

परिणाम:

UIButton + iOS Keyboard style

+0

यह काम करता है लेकिन यदि आप कस्टम कोड में इस कोड का उपयोग करते हैं, तो सहायक स्पर्श अंतराल होगा। – TomSawyer

+0

यह सुनिश्चित नहीं है कि आपको यह काम करने के लिए कैसे मिला या यदि आपने अपना उत्तर पोस्ट करने के बाद कुछ बदल दिया है। जब मैं इसका परीक्षण करता हूं तो मुझे कोने त्रिज्या दिखाई नहीं देता है। केवल अगर मैं layer.masksToBounds या क्लिप सेट करता हूं तो सही करने के लिए मैं कोने त्रिज्या देखता हूं और फिर कोई छाया नहीं। एक्सकोड 8, स्विफ्ट 3, आईओएस 10 में परीक्षण किया गया। –

+0

@MarkMoeykens ठीक है, मैंने आईओएस 10.2 के साथ एक्सकोड 8.2 पर एक ही सटीक कोड का प्रयास किया और यह काम कर रहा है। क्या आप वाकई कुछ याद नहीं कर रहे हैं? – ricardopereira

9

आप इस कोड के साथ की कोशिश कर सकते हैं:। (क्षमा करें, मैं केवल तेजी से जानते हैं, ग obj नहीं इस कोड को अपने बटन पर नीचे छाया जोड़ देगा

button.layer.masksToBounds = false 
button.layer.shadowColor = UIColor(rgb: 0x000000, alpha: 1.0).CGColor 
button.layer.shadowOpacity = 1.0 
button.layer.shadowRadius = 0 
button.layer.shadowOffset = CGSizeMake(0, 1.0) 
18

भी 0 के shadowRadius स्थापित करने के लिए सुनिश्चित करें।:

self.button.layer.shadowColor = [UIColor grayColor].CGColor; 
self.button.layer.shadowOffset = CGSizeMake(0, 1.0); 
self.button.layer.shadowOpacity = 1.0; 
self.button.layer.shadowRadius = 0.0; 
2

CornerRadius और shado:

को देखते हुए एक UIButton संपत्ति button नामित इस तरह अपने समर्थन परत गुण सेट डब्ल्यू एक ही परत पर अच्छी तरह से मिश्रण नहीं है। इसलिए आपको UIView के अंदर अपना "कोने वाला" UIButton एम्बेड करना होगा। इस UIView पर छाया लागू की जाएगी।

निम्नलिखित कोड, एक उदाहरण के रूप में दिया जाता है, यह नीचे दी गई छवि का उत्पादन:

आयात UIKit

class CustomButton: UIButton { 

    required init(coder decoder: NSCoder) { 
     super.init(coder: decoder) 

     backgroundColor = UIColor.whiteColor() 
    } 

    override func drawRect(rect: CGRect) { 
     updateLayerProperties() 
    } 

    func updateLayerProperties() { 
     layer.masksToBounds = true 
     layer.cornerRadius = 12.0 

     //superview is your optional embedding UIView 
     if let superview = superview { 
      superview.backgroundColor = UIColor.clearColor() 
      superview.layer.shadowColor = UIColor.darkGrayColor().CGColor 
      superview.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: 12.0).CGPath 
      superview.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) 
      superview.layer.shadowOpacity = 1.0 
      superview.layer.shadowRadius = 2 
      superview.layer.masksToBounds = true 
      superview.clipsToBounds = false 
     } 
    } 

} 
8

तेज 2.0 में कोड के साथ छाया UIView (UIButton) जोड़ने के वर्ग घोषणा से पहले या तेज फ़ाइल कार्यों में:

extension UIView { 

    func addShadowView(width:CGFloat=0.2, height:CGFloat=0.2, Opacidade:Float=0.7, maskToBounds:Bool=false, radius:CGFloat=0.5){ 
     self.layer.shadowColor = UIColor.blackColor().CGColor 
     self.layer.shadowOffset = CGSize(width: width, height: height) 
     self.layer.shadowRadius = radius 
     self.layer.shadowOpacity = Opacidade 
     self.layer.masksToBounds = maskToBounds 
    } 

} 

viewDidLoad में इस कोड

button.addShadowView() 
16

स्विफ्ट 3

import UIKit 

class ButtonWithShadow: UIButton { 

    override func draw(_ rect: CGRect) { 
     updateLayerProperties() 
    } 

    func updateLayerProperties() { 
     self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor 
     self.layer.shadowOffset = CGSize(width: 0, height: 3) 
     self.layer.shadowOpacity = 1.0 
     self.layer.shadowRadius = 10.0 
     self.layer.masksToBounds = false 
    } 

} 
जोड़ने

button without rounded corners with shadow

!! केवल तभी जब आपको कोने त्रिज्या और छाया की आवश्यकता नहीं होती है।अन्यथा this देखें।