frame和bounds浅析

一、frame

改变的始终是自己的位置大小,是以父视图的坐标为参照,从而确定在父视图中的位置大小。

二、bounds

  • 在设置view1的bounds(-20,-20)以后,view1的子视图view2的frame设置在(0,0)点,但却发生了偏移,因为bounds改变的是子视图的位置,改变的是自身的坐标原点,默认原点在自身的左上角。
    现在view1的左上角坐标是(-20,-20),view2的(0,0)点就要往右往下正向移动20px。
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    view1.bounds = CGRectMake(-20, -20, 200, 200);
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor blueColor];
    [view1 addSubview:view2];
image.png
  • 注意:改变bounds的大小,当前视图的中心点不会改变,大小会改变,看起来有缩放效果
    view1.bounds = CGRectMake(-20, -20, 100, 100);
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。