Why masksToBounds = YES prevents CALayer shadow?

后端 未结 6 1258
借酒劲吻你
借酒劲吻你 2020-11-28 01:44

With the following snippet, I\'m adding a drop shadow effect to one my UIView. Which works pretty well. But as soon as I set the view\'s masksToBounds prope

6条回答
  •  忘掉有多难
    2020-11-28 02:28

    Swift 3.0 version with StoryBoard

    The same idea with @TheSquad. Create a new view under the actual view and add shadow to the lower view.

    1. Create a view under the actual view

    Drag a UIView to StoryBoard with same constraint as your target view. Check clip to bound for the target view. Also make sure the new view is listed before the target view so that the target view will cover the new view.

    2. Now link the new view to your code add add shadow on it

    This is just a sample. You can do whatever way you want here

    shadowView.layer.masksToBounds = false
    shadowView.layer.shadowColor = UIColor.red.cgColor
    shadowView.layer.shadowOpacity = 0.5
    shadowView.layer.shadowOffset = CGSize(width: -1, height: 1)
    shadowView.layer.shadowRadius = 3
    
    shadowView.layer.shadowPath = UIBezierPath(rect: coverImage.bounds).cgPath
    shadowView.layer.shouldRasterize = true
    

提交回复
热议问题