框架和边界有什么区别

框架和边界有什么区别

本文介绍了可可:框架和边界有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIView 及其子类都具有framebounds 属性.有什么区别?

UIView and its subclasses all have the properties frame and bounds. What's the difference?

推荐答案

矩形,表示为相对于自身坐标的位置 (x,y) 和大小 (width,height)系统 (0,0).

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

框架noreferrer">UIView矩形,表示为相对于它所包含的超视图的位置 (x,y) 和大小 (width,height).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

因此,想象一个大小为 100x100(宽 x 高)的视图,位于其父视图的 25,25 (x,y) 处.以下代码打印出此视图的边界和框架:

So, imagine a view that has a size of 100x100 (width x height) positioned at 25,25 (x,y) of its superview. The following code prints out this view's bounds and frame:

// This method is in the view controller of the superview
- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
    NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
    NSLog(@"bounds.size.width: %f", label.bounds.size.width);
    NSLog(@"bounds.size.height: %f", label.bounds.size.height);

    NSLog(@"frame.origin.x: %f", label.frame.origin.x);
    NSLog(@"frame.origin.y: %f", label.frame.origin.y);
    NSLog(@"frame.size.width: %f", label.frame.size.width);
    NSLog(@"frame.size.height: %f", label.frame.size.height);
}

这段代码的输出是:

bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 100
bounds.size.height: 100

frame.origin.x: 25
frame.origin.y: 25
frame.size.width: 100
frame.size.height: 100

所以,我们可以看到,在这两种情况下,无论我们是查看边界还是框架,视图的宽度和高度都是相同的.不同的是视图的 x,y 定位.在边界的情况下,x 和 y 坐标为 0,0,因为这些坐标是相对于视图本身的.然而,框架 x 和 y 坐标是相对于父视图中视图的位置(之前我们说过是 25,25).

So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).

还有一个涵盖 UIViews 的精彩演示.请参阅幻灯片 1-20,它不仅解释了框架和边界之间的区别,而且还展示了视觉示例.

There is also a great presentation that covers UIViews. See slides 1-20 which not only explain the difference between frames and bounds but also show visual examples.

这篇关于可可:框架和边界有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:48