iosswiftxcodeuiviewradius

Swift Shadow and corner radius not visible


I want a view with some shadow and corner radius the problem i have when i have shadow the corner radius is not available i set masktobound to false and i have added this code

  func setShadow() {
    cardCBView.layer.cornerRadius = 10
    cardCBView.layer.borderWidth = 1.0
    cardCBView.layer.borderColor = #colorLiteral(red: 0.8980392157, green: 0.8980392157, blue: 0.8980392157, alpha: 0)
    cardCBView.layer.shadowColor = UIColor.black.cgColor
    cardCBView.layer.shadowOffset = CGSize.zero
    cardCBView.layer.shadowOpacity = 0.2
    cardCBView.layer.shadowRadius = 4.0
 }

i have result like image : with masktobound off with masktobound off

with masktobound = on with masktobound = on

when i put different color for bordercolor when i put different color for bordercolor

how can i have shadow and radius corner ?

any help is appreciated


Solution

  • There are three steps through which you can achieve both shadow and corers

    1. Add one more view behind CardCB View with same frame or constraints
    2. Add corners to this view ad set clipToBounds = true
    3. Give your main view shadow and set its clipToBounds = false