UIButton bottom shadow

后端 未结 11 1135
-上瘾入骨i
-上瘾入骨i 2020-12-12 14:14

I have a UIButton which is very similar to the standard iOS keyboard alphabet button.

I am not sure how to create a shadow only for the bottom layer li

相关标签:
11条回答
  • 2020-12-12 14:40

    You can mix the cornerRadius and shadow properties. I tested it on iOS 8.

    Objective-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;
    

    Swift:

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

    Result:

    UIButton + iOS Keyboard style

    0 讨论(0)
  • 2020-12-12 14:43

    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
        }
    
    }
    

    !! Only if you do not need corner radius and shadow simultaneously. Otherwise watch this.

    0 讨论(0)
  • 2020-12-12 14:43

    To add shadow to a button with corner radius:

    final class CustomButton: UIButton {
    
        private var shadowLayer: CAShapeLayer!
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            if shadowLayer == nil {
                shadowLayer = CAShapeLayer()
                shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 33).cgPath
                if self.backgroundColor != nil {
                    shadowLayer.fillColor = self.backgroundColor?.cgColor
                }
                else{
                    shadowLayer.fillColor = UIColor.white.cgColor
                }
                shadowLayer.shadowColor = UIColor.gray.cgColor
                shadowLayer.shadowPath = shadowLayer.path
                shadowLayer.shadowOffset = CGSize(width: 0.0, height: 3.0)
                shadowLayer.shadowOpacity = 0.4
                shadowLayer.shadowRadius = 2
    
                layer.insertSublayer(shadowLayer, at: 0)
    
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 14:45

    Be sure to also set shadowRadius to 0:

    Given a UIButton property named button set its backing layer properties like this:

    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;
    
    0 讨论(0)
  • 2020-12-12 14:47

    in swift 2.0 add shadow uiview (uibutton) with code before class declaration or in swift file functions:

    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
        }
    
    }
    

    in viewdidload add this code

    button.addShadowView()
    
    0 讨论(0)
  • 2020-12-12 14:48

    You can try with this code: (sorry, i only know swift, not obj c. this code will add bottom shadow on your button.

    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)
    
    0 讨论(0)
提交回复
热议问题