Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

masksToBounds vs. clipsToBounds [duplicate]

From my point of view both UIView clipsToBounds and CALayer masksToBounds do the same job.

I couldn't find any difference between them.

Can somebody kindly explain how they are different?

like image 525
sathishkumar_kingmaker Avatar asked Sep 13 '16 08:09

sathishkumar_kingmaker


People also ask

What does Maskstobounds mean?

A Boolean indicating whether sublayers are clipped to the layer's bounds.

What is Clipstobounds?

A Boolean value that determines whether subviews are confined to the bounds of the view.


2 Answers

masksToBounds

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.

When the value of this property is true, Core Animation creates an implicit clipping mask that matches the bounds of the layer and includes any corner radius effects. If a value for the mask property is also specified, the two masks are multiplied to get the final mask value.

you can get the more information in API Reference.

clipToBounds

The use case for clipsToBounds is more for subviews which are partially outside the main view. For example, I have a (circular) subview on the edge of its parent (rectangular) UIView. If you set clipsToBounds to YES, only half the circle/subview will be shown. If set to NO, the whole circle will show up. Just encountered this so wanted to share

for more information sample link

like image 195
Anbu.Karthik Avatar answered Oct 04 '22 14:10

Anbu.Karthik


clipsToBounds : with clipsToBounds set to YES, I'll only see the part of the subview that fits within the bounds of the superview. Otherwise, if clipsToBounds is set to NO, I'll see the entire subview, even the parts outside the superview 

masksToBounds: 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 20
Bhagabata Avatar answered Oct 04 '22 14:10

Bhagabata