clipsToBounds
A Boolean value that indicates whether the view, and its subviews, confine their drawing areas to the bounds of the view.
Declaration
var clipsToBounds: Bool { get set }Discussion
Setting this value to true causes the view, and its subviews, to clip themselves to the bounds of the view. Setting it to false prevents views and subviews whose frames extend beyond the visible bounds of the view from clipping themselves. A value of false has no effect on hitTest(_:) but does affect visibleRect, as well as the area drawn inside draw(_:).
By default this value is false. In macOS 13 and earlier, the default value is true.
Because of this change in default value, views built on macOS 13 and earlier may require layout adjustments such as the following on newer versions of macOS:
Showing or hiding UI elements by setting a parent’s frame size to zero. To hide a view hierarchy by shrinking the parent view, or positioning a child view outside a parent’s bounds, set the clipsToBounds property of the parent view to true. Alternatively, set isHidden to true on the parent view instead.
Filling the
dirtyRectof a view inside draw(_:). It’s a common practice to set the background color on a view by calling setFill() on a background color and then calling fill(using:) on thedirtyRectparameter passed into an override of draw(_:). Because thedirtyRectnow extends outside your view’s bounds, call fill(using:) on the view’s bounds instead of thedirtyRect, or set the view’s clipsToBounds to true.Differentiating a view’s bounds from its
dirtyRect. Use thedirtyRectparameter passed to draw(_:) to determine what to draw, not where to draw it. Use the view’s bounds to determine the layout of what your view draws.