iOS Frame与Bounds的区别和使用

本文为博主手写总结性文章,如若涉及版权问题,请与博主联系。

官方文档:
Frame:
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.

This rectangle defines the size and position of the view in its superview’s coordinate system. Use this rectangle during layout operations to set the size and position the view. Setting this property changes the point specified by the center property and changes the size in the bounds rectangle accordingly. The coordinates of the frame rectangle are always specified in points.

框架矩形,用于描述超视图坐标系中视图的位置和大小。

此矩形在其superview的坐标系中定义视图的大小和位置。在布局操作期间使用此矩形来设置视图的大小和位置。设置此属性会更改center属性指定的点,并相应地更改bounds矩形中的大小。框架矩形的坐标始终以磅为单位指定。

Bounds:
The bounds rectangle, which describes the view’s location and size in its own coordinate system.

The default bounds origin is (0,0) and the size is the same as the size of the rectangle in the frame property. Changing the size portion of this rectangle grows or shrinks the view relative to its center point. Changing the size also changes the size of the rectangle in the frame property to match. The coordinates of the bounds rectangle are always specified in points.

Changing the bounds rectangle automatically redisplays the view without calling its drawRect: method. If you want UIKit to call the drawRect: method, set the contentMode property to UIViewContentModeRedraw.

Changes to this property can be animated.

边界矩形,用于描述视图在其自身坐标系中的位置和大小。

默认边界原点为(0,0),其大小与frame属性中矩形的大小相同。更改此矩形的大小部分会相对于其中心点增大或缩小视图。更改大小还会更改frame属性中矩形的大小以匹配。边界矩形的坐标始终以磅为单位指定。

更改边界矩形会自动重新显示视图而不调用其drawRect:方法。如果您希望UIKit调用drawRect:方法,请将contentMode属性设置为UIViewContentModeRedraw。

可以对此属性的更改进行动画处理。

相关问题点总结:

  • Frame的坐标是相对于父视图坐标系的,所以设置坐标的时候要知道父视图是哪一个,简单的寻找父视图的方法是看这个view被谁addSubview了.
  • Bounds描述的是当前视图相对于自身坐标系的位置和大小.
  • Bounds可以修改自己坐标系的原点位置,进而影想到“子view”的显示位置。这个作用更像是移动原点的意思.
  • Bounds可以改变的frame。如果bounds比frame大。那么frame也会跟着变大。这个作用更像边界和大小的意思.
  • Frame和Bounds的width和height是一样的,唯独坐标是不一样的,frame的坐标是相对于 父视图的,bounds的坐标是相对于自身的(只要不进行设置一直都是(0,0)).
  • setBounds中的(x,y)只改变自己的坐标系统,子View的bounds和frame并不会改变
  • setBounds是修改自己坐标系的原点位置,进而影响到子View的显示位置.

代码实例:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height
        
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight/3))
        view1.backgroundColor = UIColor.blue
        print("view1.frame:",view1.frame)
        print("view1.bounds:",view1.bounds)
        self.view.addSubview(view1)
        
        let view2 = UIView(frame: CGRect(x: 0, y: screenHeight/3, width: screenWidth, height: screenHeight/3))
        view2.backgroundColor = UIColor.red
        print("view2.frame:",view2.frame)
        print("view2.bounds:",view2.bounds)
        self.view.addSubview(view2)
        
        let view3 = UIView(frame: CGRect(x: 0, y: view2.frame.maxY, width: screenWidth, height: screenHeight/3))
        view3.backgroundColor = UIColor.yellow
        print("view3.frame:",view3.frame)
        print("view3.bounds:",view3.bounds)
        self.view.addSubview(view3)
        
        let view4 = UIView()
        view4.frame = CGRect(x: view3.bounds.minX, y: view3.bounds.minY, width: 100, height: 100)
        view4.backgroundColor = UIColor.brown
        print("view4.frame:",view4.frame)
        print("view4.bounds:",view4.bounds)
        view3.addSubview(view4)
    }


}

输出结果:
view1.frame: (0.0, 0.0, 375.0, 270.6666666666667)
view1.bounds: (0.0, 0.0, 375.0, 270.6666666666667)
view2.frame: (0.0, 270.66666666666663, 375.0, 270.6666666666667)
view2.bounds: (0.0, 0.0, 375.0, 270.6666666666667)
view3.frame: (0.0, 541.3333333333333, 375.0, 270.6666666666667)
view3.bounds: (0.0, 0.0, 375.0, 270.6666666666667)
view4.frame: (0.0, 0.0, 100.0, 100.0)
view4.bounds: (0.0, 0.0, 100.0, 100.0)

如果将view3.frame与view3.bounds做对比可以得到:
view3.frame: (0.0, 541.3333333333333, 375.0, 270.6666666666667)
view3.bounds: (0.0, 0.0, 375.0, 270.6666666666667)

代码实例:
我以self.view的原点(0,0)为起点,设置一个frame为(20,20)的点,通过bounds修改self.view的原点(0,0)为(-20,-20),查看点的frame值,点的值依旧为(20,20),但位置进行了偏移(偏移量为20).

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];//添加到self.view
    NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    view2.backgroundColor = [UIColor blueColor];
    [view1 addSubview:view2];//添加到self.view
    NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
    
    
   // [view1 setBounds:CGRectMake(-20, -20, 200, 200)];
    NSLog(@"view1 frame:%@========view1 bounds:%@",NSStringFromCGRect(view1.frame),NSStringFromCGRect(view1.bounds));
    NSLog(@"view2 frame:%@========view2 bounds:%@",NSStringFromCGRect(view2.frame),NSStringFromCGRect(view2.bounds));
}


@end

输出结果:
2019-07-08 11:09:36.540522+0800 ceshi[1010:222886] view1 frame:{{0, 0}, {200, 200}}========view1 bounds:{{0, 0}, {200, 200}}
2019-07-08 11:09:36.540605+0800 ceshi[1010:222886] view2 frame:{{20, 20}, {100, 100}}========view2 bounds:{{0, 0}, {100, 100}}
2019-07-08 11:09:36.540653+0800 ceshi[1010:222886] view1 frame:{{0, 0}, {200, 200}}========view1 bounds:{{-20, -20}, {200, 200}}
2019-07-08 11:09:36.540675+0800 ceshi[1010:222886] view2 frame:{{20, 20}, {100, 100}}========view2 bounds:{{0, 0}, {100, 100}}

个人觉得这篇文章相当不错,有兴趣可以看看,我也给自己留一个连接:
https://blog.csdn.net/u011752619/article/details/79262554

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敛柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值