Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What UIView layer.masksToBounds is doing if set to YES?

Tags:

iphone

uiview

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

like image 470
Ondrej Rafaj Avatar asked Apr 15 '10 20:04

Ondrej Rafaj


2 Answers

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).

like image 64
Noah Witherspoon Avatar answered Oct 17 '22 21:10

Noah Witherspoon


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.

like image 40
Jaykant Avatar answered Oct 17 '22 21:10

Jaykant