可可:帧和边界有什么区别? [英] Cocoa: What's the difference between the frame and the bounds?

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

问题描述

UIView 及其子类都具有属性 frame bounds 。有什么区别?

解决方案

UIView rectangle =http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html =nofollow noreferrer> UIView 是 rectangle ,表示为相对于它所包含的超级视图的位置(x,y)和大小(宽度,高度)。



因此,假设一个视图的大小100x100(宽x高),位于其超级视图的25,25(x,y)。下面的代码打印出这个视图的边界和框架:

  //这个方法在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)。



涵盖UIViews的精彩演示。请参阅幻灯片1-20,这不仅解释框架和边界之间的差异,而且还显示视觉示例。


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

解决方案

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

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.

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);
}

And the output of this code is:

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

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

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.

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

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