Reputation: 16715
I'm attempting to create a method to configure buttons in an app. My method performs as expected, but the shadows don't draw. It does not crash.
I've created a UIButton
extension
with the following method:
func configureWhiteText(withBackgroundColor buttonColor: UIColor?) {
// basic configuration
tintColor = UIColor.white
setTitleColor(UIColor.white, for: .normal)
backgroundColor = buttonColor
// layer stuff
layer.drawsAsynchronously = true
layer.shouldRasterize = true
layer.cornerRadius = 10.0
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
layer.shadowRadius = 5.0
}
I've tried it within a UITableViewController
in viewDidLoad
and 'viewWillAppear` without luck:
myButton.configureWhiteText(withBackgroundColor: UIColor.blue)
The buttons are in grouped static cells of UITableViewController
. Everything works within the configuration method except the shadow-oriented lines of code. layer.cornerRadius
rounds the corner as expected, but none of the other layer
-oriented lines draw a shadow.
I've verified layer.masksToBounds = false
. I also tried commenting out layer.drawsAsynchronously
and layer.shouldRasterize
to no avail.
Any further ideas/suggestions are appreciated.
Upvotes: 1
Views: 2419
Reputation: 21
For someone, like me, who might still face the same issue even if layer.shadowOpacity is set to a non-zero value. If your button background color is UIColor.clear, you should change it to other colors, e.g. white color.
Upvotes: 0
Reputation: 534893
You left out the setting that actually turns on the shadow — the shadowOpacity
. Add this line:
layer.shadowOpacity = 0.5
Badda bing badda boom.
Upvotes: 8
Reputation: 1403
You need to set the shadow path, assuming it is in an uibutton extension
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 20).CGPath
Upvotes: 1