44

Anyone know? I found a few answers, but there were too complex and going too deep. I need a simple answer.

demongolem
  • 9,148
  • 36
  • 86
  • 104
Ondrej Rafaj
  • 4,202
  • 8
  • 40
  • 64

2 Answers2

145

If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).

Noah Witherspoon
  • 56,657
  • 16
  • 129
  • 131
5

Input Design in storyboard

enter image description here

@IBOutlet weak var purpleView: UIView!  // view inside super view
@IBOutlet weak var yellowView: UIView!  // super view

override func viewDidLoad() {
    super.viewDidLoad()

    yellowView.layer.cornerRadius = 20
    yellowView.layer.masksToBounds = true
    
    // Do any additional setup after loading the view.
}

enter image description here

output after maskToBounds = true. Super view clip the subview's part which are outside the superview.

Jaykant
  • 219
  • 4
  • 9