UIView的frame和bounds区别

iOS中,大家肯定对view和frame都不陌生,我们设置view在父view中的位置和大小时,只需要设置frame就可以了。

可能大家也有查过网上的一些资料,可能也会得知frame是相对于superview坐标系的,而bounds则相对于view自身的坐标系,但是frame究竟是怎样表示坐标的呢,其实这就和bounds有关了。

值得肯定的是frame.size 和bounds.size 肯定一样,但是UIView中,frame其实是不存储的,而是动态计算的,改变center,改变bounds大小,或者改变transfrom都可能会导致frame的改变。

UIView的frame是一个动态的,官方文档中有提到

// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.

@property(nonatomic)
CGRect frame;

大意是说,如果我们的view有transform,则frame不能反映其在父view中的实际位置,需要用bounds+center来反映。

假定我们的view没有transform。 每次我们设置frame时,则系统首先会设置

bounds.origin
= 0,0

bounds.size = frame.size

然后会继续设置

center.x
= frame.origin.x + frame.size.width / 2

center.y = frame.origin.y + frame.size.height / 2

也就是说 设置frame时,我们的bounds和center都会发生变化,但是view的位置是由center和bounds共同决定的,完全可以不依赖于frame。

每次我们访问frame时,其实也是通过center和bounds计算。

frame.size
= bounds.size

frame.origin.x = center.x - bounds.size.width / 2

frame.origin.y = center.y - bounds.size.height / 2

当然期间如果有transform,则会考虑到transfrom,比如大小缩小到0.9倍,则center不变,frame.size 变为 0.9倍,origin也会跟着变。 transform不会影响到view的bounds和center,但是会影响到frame。

版权声明:本文为博主原创文章,未经博主允许不得转载。

UIView的frame和bounds区别的更多相关文章

  1. ios开发之UIView的frame、bounds跟center属性的区别(附图)

    博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...

  2. iOS--------坐标系统(UIView的frame、bounds跟center属性)

    1.概要翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下: frame:描述当前视图在其父视图中的位置和大小. bounds:描述当前视图在其自身坐标系统中的位置和大小. center:描述 ...

  3. 详解UIView的frame、bounds和center属性

    From: http://ios.wpjam.com/2011/08/29/uiview-frame-bounds-center/ 1.概要 翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下 ...

  4. frame和bounds区别

    学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bound的 ...

  5. [UIKit学习]01.关于UIView,frame,bounds,center

    UIView是Cocoa大多控件的父类,本身不带事件. UIView的常见用法 @property(nonatomic,readonly) UIView *superview; 获得自己的父控件对象 ...

  6. iOS开发-View中frame和bounds区别

    开发中调整View的时候的经常会遇到frame和bounds,刚开始看的时候不是很清楚,不过看了一下官方文档,frame是确定视图在父视图中的位置,和本身的大小,bounds确定可以确定子视图在当前视 ...

  7. UIView的frame和bounds的含义

    1.frame是该view相对于父view的坐标系中的位置和大小.(参照点是父view的坐标系) 2.bounds是该view相对于自己的坐标.(参照点是本身坐标系统) 3.uiresponder&l ...

  8. iOS 基础-----关于UIView 的 frame 与 bounds

    首先,对于frame 大家都很熟悉,是当前view ,相对于其父视图view 的坐标,例如: UIView *view1 = [[UIView alloc] initWithFrame:CGRectM ...

  9. IOS之frame和bounds区别

    用最简单的语言来解释就是:setFrame和setBounds都是为了把子view加载到父view上去,但设置的参数坐标系不同,setFrame是该view在父view坐标系统中的位置和大小,setB ...

随机推荐

  1. Web---myAjax(自己写底层)-隐藏帧技术

    讲解网站一般都有的一个功能,就是注册时候的,实现验证用户名是否存在的功能. 源代码演示: reg.jsp: <%@ page language="java" import=& ...

  2. marvell笔试题(嵌入式软件)

    有幸去Marvell参加面试,由于其要求WCDMA/GSM之类的,我还特地恶补了下这方面的知识.后来坐了2个小时的地铁后,到达了Marvell.公司还蛮不错的,里面环境都还可以.我投了这家公司也是同学 ...

  3. sed命令教程

    转载自:http://coolshell.cn/articles/9104.htmlawk于1977年出生,今年36岁本命年,sed比awk大2-3岁,awk就像林妹妹,sed就是宝玉哥哥了.所以 林 ...

  4. centos 7 安装mp3解码器

    1. Install the nux repo  $> su - $> yum update # optional but recommanded $> rpm --import h ...

  5. windows service自动重启服务

    服务一般都能正常的运行,但有时候也会有一些假死现象,比如公司有一考勤服务就因为依赖于硬件厂家的api, 但厂家api运行一段时间后会默名的假死,引起整个服务假死,因为这一假死现象具有不确定性,所以不太 ...

  6. javascript闭包问题

    <script type="text/javascript"> window.onload = function(){ var name = "The Win ...

  7. java Graphics2D 画图

    在Java中,当需要画一些特殊的形状时,比如说椭圆.矩形等,可以使用 Graphics2D 来绘图. 一些API: g.drawLine(3,3,50,50);//画一条线段 g.drawRect(8 ...

  8. 第一个java程序

    完成自己的第一个java程序 1.新建一个文本文档,在文本文档中编写自己第一个java程序的代码,代码如下; class hello { public static void main(String[ ...

  9. TreeMap 排序

    一.TreeMap TreeMap 默认排序规则:按照key的字典顺序来排序(升序) 当然,也可以自定义排序规则:要实现Comparator接口. 用法简单,先看下下面的demo public cla ...

  10. 什么是Scale Up和Scale Out?

    导读:Scale Out(也就是Scale horizontally)横向扩展,向外扩展 Scale Up(也就是Scale vertically)纵向扩展,向上扩展 无论是Scale Out,Sca ...