tamtoum1987
tamtoum1987

Reputation: 2057

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

Upvotes: 1

Views: 2727

Answers (3)

Mike zhao
Mike zhao

Reputation: 21

There is a way using CAShapeLayer as background to do it

    let bgLayer = CAShapeLayer()
    bgLayer.frame = bounds
    bgLayer.shadowOffset = CGSize(width: 0, height: -1)
    bgLayer.shadowOpacity = 0.8
    bgLayer.shadowRadius = 1.0
    
    bgLayer.shadowColor = UIColor.shadowBackground.cgColor
    bgLayer.fillColor = UIColor.orderPopBg.cgColor
    
    let path = CGMutablePath()
    path.addRoundedRect(in: bounds, cornerWidth: 10, cornerHeight: 10)
    bgLayer.path = path
    layer.addSublayer(bgLayer)

Change the shadowColor and fillColor to yours.

Upvotes: 0

Jawad Ali
Jawad Ali

Reputation: 14397

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

Upvotes: 1

mmm
mmm

Reputation: 268

You should set

clipsToBounds = true

Upvotes: -2

Related Questions