美文网首页
iOS中frame和bounds区别

iOS中frame和bounds区别

作者: 罗平油菜花 | 来源:发表于2018-12-13 16:22 被阅读0次

    frame指的是:该view在父view坐标系统中的位置和大小。(参照点是父亲的坐标系统)

    bounds指的是:该view在本身坐标系统中 的位置和大小。(参照点是本身坐标系统)

    以下是代码,方便理解

    
    UIView *superView = [[UIView alloc]initWithFrame:CGRectMake(20,20,200,200)];
    
        superView.backgroundColor = [UIColor redColor];
    
        [self.view addSubview:superView];
    
        NSLog(@"superView frame:%@========superView bounds:%@",NSStringFromCGRect(superView.frame),NSStringFromCGRect(superView.bounds));
    
        UIView*subView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];
    
        subView.backgroundColor = [UIColor orangeColor];
    
        [superView addSubview:subView];
    
        NSLog(@"subView frame:%@========subView bounds:%@",NSStringFromCGRect(subView.frame),NSStringFromCGRect(subView.bounds));
    
    

    控制台打印结果:

    
    superView frame:{{20, 20}, {200, 200}}========superView bounds:{{0, 0}, {200, 200}}
    
    subView frame:{{0, 0}, {100, 100}}========subView bounds:{{0, 0}, {100, 100}}
    
    

    运行结果如图一

    image

    下面我们改变superView的bounds来看看子视图会发生什么变化。

    我们在上面定义superView的时候,修改superView的bounds。

    
        [superView setBounds:CGRectMake(-20, -20,200,200)];
    
    

    控制台打印结果:

    
      superView frame:{{20, 20}, {200, 200}}========superView bounds:{{-20, -20}, {200, 200}}
    
     subView frame:{{0, 0}, {100, 100}}========subView bounds:{{0, 0}, {100, 100}}
    
    

    运行结果如图二

    image

    我们在改变父视图的bounds的时候,子视图必将受影响。因为子视图的frame是根据父视图的bounds来确定的。

    希望能帮助到你。

    Best regards

    Roger

    相关文章

      网友评论

          本文标题:iOS中frame和bounds区别

          本文链接:https://www.haomeiwen.com/subject/hrjbhqtx.html