Swift - Problems with corner radius and drop shadow

前端 未结 14 1432
失恋的感觉
失恋的感觉 2020-11-28 18:35

I\'m trying to create a button with rounded corners and a drop shadow. No matter how I switch up, the button will not display correctly. I\

14条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 18:49

    My custom button with some shadow and rounded corners, I use it directly within the Storyboard with no need to touch it programmatically.

    Swift 4

    class RoundedButtonWithShadow: UIButton {
        override func awakeFromNib() {
            super.awakeFromNib()
            self.layer.masksToBounds = false
            self.layer.cornerRadius = self.frame.height/2
            self.layer.shadowColor = UIColor.black.cgColor
            self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
            self.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
            self.layer.shadowOpacity = 0.5
            self.layer.shadowRadius = 1.0
        }
    }
    

提交回复
热议问题