0

UIView Custom Class trigger only shadow. I want to display both shadow and rounded corner for specific edges.Here is my code. Any suggestions would be appreciated.

    extension UIView {
        func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}
    class CustomView: UIView {
     override func layoutSubviews() {
            self.shadowColor = UIColor.darkGray
            self.shadowOffset = CGSize(width: 4, height: 4)
            self.shadowOpacity = 0.5
            self.shadowRadius = 6.0
            //only rounded corners triggered in UI
            self.backgroundColor = UIColor.white
            self.roundCorners(corners: [.topRight, .bottomRight], radius: 15.0)
        }
    }

Thanks in advance

yazh
  • 701
  • 10
  • 15
  • round corner requires clipToBounds property true while shadow requires clipToBound property false. – MRizwan33 Mar 28 '18 at 12:39
  • use two views one to give shadow and other for cornerRadius. test to write them above or below one another methods. – MRizwan33 Mar 28 '18 at 12:41
  • Might help https://stackoverflow.com/questions/4754392/uiview-with-rounded-corners-and-drop-shadow – Vinaykrishnan Mar 28 '18 at 12:41
  • @MRizwan33 Then how can i achieve this? can you tell me> – yazh Mar 28 '18 at 12:41
  • answer given below you can get idea. if it is correct than accept it otherwise commit below. – MRizwan33 Mar 28 '18 at 12:48
  • @MRizwan33 Then how can i achieve this? can you tell me? i already have UIButton within UIView but UIButton throws error for shadow and does not trigger backButton.roundCorners([.topRight, .bottomRight], radius: 15.0) this method – yazh Mar 28 '18 at 12:49
  • in your above code there is no button used. share your actual code. – MRizwan33 Mar 28 '18 at 12:51

2 Answers2

0

Use separate views for the shadow and the border. The base view is transparent and has the shadow. The border view clips any other subcontent that it has to its borders. See the code below.

// add the shadow to the base view

baseView.backgroundColor = UIColor.clear
baseView.layer.shadowColor = UIColor.black.cgColor
baseView.layer.shadowOffset = CGSize(width: 3, height: 3)
baseView.layer.shadowOpacity = 0.7
baseView.layer.shadowRadius = 4.0

// add the border to subview

let borderView = UIView()
borderView.frame = baseView.bounds
borderView.layer.cornerRadius = 10
borderView.layer.borderColor = UIColor.black.cgColor
borderView.layer.borderWidth = 1.0
borderView.layer.masksToBounds = true
baseView.addSubview(borderView)

// add any other subcontent that you want clipped

let otherSubContent = UIImageView()
otherSubContent.image = UIImage(named: "lion")
otherSubContent.frame = borderView.bounds
borderView.addSubview(otherSubContent)
MRizwan33
  • 2,723
  • 6
  • 31
  • 42
0

Use this function:-

func addShadow(view: UIView, corner: CGFloat) {
    view.layer.shadowColor = UIColor.gray.cgColor
    view.layer.shadowOpacity = 0.5
    view.layer.shadowRadius = 2
    view.layer.shadowOffset = CGSize(width: 1, height: 1)
    view.layer.masksToBounds = false
    view.layer.cornerRadius = corner
}
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19