2014-11-22 41 views
29

我有一个UIButton这是非常类似于标准的iOS键盘字母按钮。UIButton bottom shadow

我不知道如何创建一个仅用于底层的阴影,就像iOS所做的一样。

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 2014-11-22 20:51:15

+0

我想你也想设置'shadowRadius' 0 – Aaron 2014-11-28 17:59:55

+0

另外'maskToBounds'在默认情况下是“NO”。 – Aaron 2014-11-28 18:06:39

回答

48

可以混合使用cornerRadius和阴影属性。我测试了它在iOS 8.

目的-C:

[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 2015-10-07 17:31:41

+0

不知道你是如何得到这个工作的,或者自从发布你的答案以来发生了什么变化。当我测试这个时,我看不到角落半径。只有当我将layer.masksToBounds或clipsToBounds设置为true时,才会看到角落半径,然后看不到阴影。在Xcode 8,Swift 3,iOS 10中测试过。 – 2017-05-16 17:34:21

+0

@MarkMoeykens嗯,我在iOS 10.2上尝试了Xcode 8.2上相同的确切代码,它正在工作。你确定你没有错过什么吗? – ricardopereira 2017-05-18 06:25:59

9

您可以使用此代码尝试: (对不起,我只知道SWIFT,不是OBJ C时,该代码就会在您的按钮添加底部阴影

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

同时务必设置shadowRadius为0:

鉴于命名button一个UIButton属性设置其衬里层性能是这样的:

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不要在同一层上混合好。所以你必须在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

SWIFT 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