ios clipsToBounds 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20449256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does clipsToBounds work?
提问by Toshi
I would like to know how to use the UIView
property clipsToBounds
.
我想知道如何使用该UIView
属性clipsToBounds
。
The official documentation says the following:
官方文档说如下:
clipsToBounds
propertyA Boolean value that determines whether subviews are confined to the bounds of the view.
Discussion
Setting this value toYES
causes subviews to be clipped to the bounds of the receiver. If set toNO
, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value isNO
.
clipsToBounds
财产一个布尔值,用于确定子视图是否被限制在视图的边界内。
讨论
将此值设置为YES
会导致子视图被裁剪到接收器的边界。如果设置为NO
,则不会剪裁帧超出接收器可见边界的子视图。默认值为NO
。
But I'm unclear on what this means exactly. How should I be using clipsToBounds
? What are the consequences of setting this property to YES
exactly? Or to NO
?
但我不清楚这究竟意味着什么。我应该如何使用clipsToBounds
?将此属性设置为YES
完全正确的结果是什么?或者到NO
?
回答by nhgrif
If my superview is a box measuring 10 units on each side, and my subview is 20 units wide, 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 (assuming we're still on the screen).
如果我的超级视图是一个每边 10 个单位的框,我的子视图宽 20 个单位,clipsToBounds
设置为YES
,我将只看到适合超级视图边界的子视图部分。否则,如果clipsToBounds
设置为NO
,我将看到整个子视图,甚至是超级视图之外的部分(假设我们仍在屏幕上)。
As a visual example, consider the following views set up on the storyboard:
作为视觉示例,请考虑在故事板上设置的以下视图:
This is a white UIView
, a label in the top left corner with either a simple "1" or "2" so that I can discuss these as view1
or view2
. Additionally, the black view is the same size as the white view, but it's origin is at the white view's center.
这是一个白色的UIView
,左上角的标签,带有简单的“1”或“2”,以便我可以将它们作为view1
或进行讨论view2
。此外,黑色视图与白色视图的大小相同,但它的原点在白色视图的中心。
In the view controller's viewDidLoad
method, we have the following code:
在视图控制器的viewDidLoad
方法中,我们有以下代码:
Objective-C:
目标-C:
- (void)viewDidLoad {
[super viewDidLoad];
self.view1.clipsToBounds = YES;
self.view2.clipsToBounds = NO;
}
Swift:
迅速:
override func viewDidLoad() {
super.viewDidLoad()
self.view1.clipsToBounds = true
self.view2.clipsToBounds = false
}
When we run the code and look at in the simulator or on the device, we get the following results:
当我们运行代码并在模拟器或设备上查看时,我们得到以下结果:
So, despite these views being set up identically (except clipsToBounds
), they look different. This is what clipsToBounds
does. Setting it to YES
will provide the top result, while setting it to NO
provides the bottom result.
因此,尽管这些视图的设置相同(除了clipsToBounds
),但它们看起来不同。这就是clipsToBounds
它的作用。将其设置为YES
将提供最高结果,而将其设置为NO
提供最低结果。
If we debug the view hierarchy, we can see more clearly that the black boxes both do actually extend past the boarders of the white view, but only view 2 shows this when the app is actually running:
如果我们调试视图层次结构,我们可以更清楚地看到两个黑框实际上都超出了白色视图的边界,但是当应用程序实际运行时,只有视图 2 显示了这一点:
回答by Honey
This is meant to be a succinct quick answer vs. the accepted answer
与公认的答案相比,这是一个简洁的快速答案
If Apple was to rename this property, I'd name it: clipSubviewsToBounds
.
如果 Apple 要重命名此属性,我会将其命名为:clipSubviewsToBounds
.
I just ran into a bug in our code. Where the parent view's height was set to 0
.
We expected to see none of its elements. Yet parent view's contents were showing up.
我刚刚在我们的代码中遇到了一个错误。父视图的高度设置为0
. 我们预计看不到它的任何元素。然而,父视图的内容出现了。
It was because clipToBounds
wasn't set to true
. Once that's set to true
then the subviews heights can't extend beyond 0
, so they don't show at all.
那是因为clipToBounds
没有设置为true
. 一旦设置为true
,子视图的高度就不能超出0
,所以它们根本不显示。