Reputation: 16715
I'm refactoring a Swift 3 project to Swift 4 and I've encountered a situation where buttons that previously had a shadows applied to them no longer draw a shadow.
I'm using this code to draw the shadow:
extension UIButton {
func drawShadow() {
self.layer.drawsAsynchronously = true
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize.zero
self.layer.shadowRadius = self.layer.cornerRadius
self.layer.shadowOpacity = 0.5
}
}
I call the code to draw the shadow in viewDidLayoutSubviews
using this code:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
buttonArray = [self.buttonOne, self.buttonTwo, self.buttonThree, self.buttonFour]
buttonArray.forEach { button in
button.subviews.filter{$0 is UIImageView}.forEach({ imageView in
imageView.contentMode = .scaleAspectFit
})
button.clipsToBounds = false
button.drawShadow()
}
}
I've tried addding the code in viewDidLayoutSubviews
in other spots to see if I can get it to draw. I've tried viewDidLoad
, viewWillLayoutSubviews
, viewDidAppear
, etc. I've tried nuking the storyboard and "starting over", by laying out the buttons again, cleaning derived data, nuking the simulator from both terminal and within Xcode.
The buttons are contained within a UIStackView
. I've got two horizontal stackviews containing two buttons embedded within a vertical stackview. If anyone's got any suggestions re: what else to try, I welcome your suggestions. Thank you for reading.
Upvotes: 0
Views: 142
Reputation: 980
Please try this code, I have changed the shadowOffset
func drawShadow() {
self.layer.drawsAsynchronously = true
self.layer.masksToBounds = false
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 1.0, height: 2.0)
self.layer.shadowRadius = self.layer.cornerRadius
self.layer.shadowOpacity = 0.5
}
Upvotes: 1