Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS add shadow to UIButton

Im trying to add a shadow to a button but the shadow gets added to the text of the button instead of the whole button:

enter image description here

Do I need to use code to achieve this? Or is it a sizing issue? I could add shadow size on the storyboard but I don't see where to set if it should go around the button

like image 519
breakline Avatar asked Mar 04 '18 18:03

breakline


4 Answers

It's because your button's background is transparent.
When you set shadow on a UIView which has a transparent background, then shadow applies to all his subviews (instead of around UIView's itself). In your case, you have a UIButton which has a transparent background, so the shadow is applied to all its visible subviews which in this case is only its titleLabel.
In other words, the system captures the non-transparent pixels of your view, tints it the shadowColor and places that image below the view with the shadowOffset.

So you have two solutions here:

  1. Change your button's background color to smth else
  2. Directly specify the shadowPath:
    button.layer.shadowPath = UIBezierPath(rect: button.layer.bounds).cgPath
like image 135
arturdev Avatar answered Oct 19 '22 19:10

arturdev


I don't have much idea about storyboard but using below layer properties you can set shadow. You can play around the button.

I try the below code and change values based on your requirement and give your button some color

    //button is your button name
    button.backgroundColor = self.view.backgroundColor
    button.layer.shadowOpacity = 0.3
    button.layer.shadowRadius = 2.0
    button.layer.shadowColor = UIColor.yellow.cgColor
    button.layer.cornerRadius = 2
like image 30
Iraniya Naynesh Avatar answered Oct 19 '22 19:10

Iraniya Naynesh


Shadow is given to the whole button. The thing is that the button itself has backgroundColor == .clear which means it does not create any shadow (only non-transparent part of it is its title, thus that's only part that creates shadow).

In your case, set the button's background color to the same color as its superview has, and you should get what you want.

like image 1
Milan Nosáľ Avatar answered Oct 19 '22 19:10

Milan Nosáľ


here is two extension methods to add a shadow

extension UIView {
func setRadiusWithShadow(_ radius: CGFloat? = nil) { // this method adds shadow to right and bottom side of button
    self.layer.cornerRadius = radius ?? self.frame.width / 2
    self.layer.shadowColor = UIColor.darkGray.cgColor
    self.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
    self.layer.shadowRadius = 1.0
    self.layer.shadowOpacity = 0.7
    self.layer.masksToBounds = false
}

func setAllSideShadow(shadowShowSize: CGFloat = 1.0) { // this method adds shadow to allsides
    let shadowSize : CGFloat = shadowShowSize
    let shadowPath = UIBezierPath(rect: CGRect(x: -shadowSize / 2,
                                               y: -shadowSize / 2,
                                               width: self.frame.size.width + shadowSize,
                                               height: self.frame.size.height + shadowSize))
    self.layer.masksToBounds = false
    self.layer.shadowColor = UIColor.lightGray.withAlphaComponent(0.8).cgColor
    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    self.layer.shadowOpacity = 0.5
    self.layer.shadowPath = shadowPath.cgPath
}
}

shadows to button

 self.view.layoutIfNeeded() // need this method to layout your run time button frame before giving shadow

first button is with all side shadow

self.recordMeasurementWrapperView.setAllSideShadow(shadowShowSize: 3.0)

second button is with shadow to only bottom and right

self.retakeMeasurementWrapperView.setRadiusWithShadow(2.0)
like image 1
Ashwin Shrestha Avatar answered Oct 19 '22 20:10

Ashwin Shrestha