clipsToBounds 是如何工作的? [英] How does clipsToBounds work?

查看:24
本文介绍了clipsToBounds 是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 UIView 属性 clipsToBounds.

官方文档如下:

<块引用>

clipsToBounds 属性

一个布尔值,用于确定子视图是否仅限于视图的边界.

讨论
将此值设置为 YES 会导致子视图被剪切到接收者的边界.如果设置为 NO,则其框架扩展的子视图超出接收器的可见边界不会被剪裁.默认的值为 NO.

但我不清楚这究竟意味着什么.我应该如何使用 clipsToBounds?将此属性设置为 YES 的后果是什么?还是NO?

解决方案

如果我的超级视图是一个边长为 10 个单位的盒子,我的子视图为 20 个单位宽,并且 clipsToBounds 设置为 YES,我只会看到子视图中适合父视图边界的部分.否则,如果 clipsToBounds 设置为 NO,我将看到整个子视图,甚至是超级视图之外的部分(假设我们仍在屏幕上).>

作为视觉示例,请考虑在故事板上设置的以下视图:

这是一个白色的 UIView,左上角的一个标签,带有一个简单的1"或2",以便我可以将它们作为 view1view2.此外,黑色视图与白色视图的大小相同,但其原点在白色视图的中心.

在视图控制器的viewDidLoad方法中,我们有以下代码:

目标-C:

- (void)viewDidLoad {[超级viewDidLoad];self.view1.clipsToBounds = YES;self.view2.clipsToBounds = NO;}

斯威夫特:

override func viewDidLoad() {super.viewDidLoad()self.view1.clipsToBounds = trueself.view2.clipsToBounds = false}

当我们运行代码并在模拟器或设备上查看时,我们得到以下结果:

因此,尽管这些视图的设置相同(clipsToBounds 除外),但它们看起来不同.这就是 clipsToBounds 所做的.将其设置为 YES 将提供顶部结果,而将其设置为 NO 提供底部结果.

如果我们调试视图层次结构,我们可以更清楚地看到黑框实际上都延伸到了白色视图的边界之外,但是当应用程序实际运行时,只有视图 2 显示了这一点:

I would like to know how to use the UIView property clipsToBounds.

The official documentation says the following:

clipsToBounds property

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

Discussion
Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is 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?

解决方案

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

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.

In the view controller's viewDidLoad method, we have the following code:

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

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:

这篇关于clipsToBounds 是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆