fram和bounds的区别和用途 - iOS - UI基础知识总结2

来源:互联网 发布:c语言的指针编程例题 编辑:程序博客网 时间:2024/05/20 20:57
// fram__________________________________
    
    // 子视图的fram是相对于父视图的,改变fram的内容会改变子视图自己在父视图中的位置和大小,但是对父视图没有影响
    // 设置fram时可以超出父视图边界范围(超出部分不会在父视图上展现)(比如:设置成(-10,-10,100,100))
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    [containerView addSubview:redView];
    [redView release];
    
    // 子视图的fram的起始位置(原点坐标)是针对父视图的 起始位置
    // 父视图的fram的改变不会影响它的子视图在它上面的 相对位置
    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];// fram里面的原点(0,0)是相对于父视图的
    blueView.backgroundColor = [UIColor blueColor];
    [redView addSubview:blueView];
    [blueView release];
    
    // 当本身的fram和center发生冲突时,fram要遵从center的改变(改变center会引起fram跟着做出相应改变)
    blueView.center = CGPointMake(50, 50);
    NSLog(@"%f , %f",blueView.center.x , blueView.center.y);
    NSLog(@"%@",blueView);
    
    // bounds__________________________________
    
    // bounds的改变对本身没有影响(自己相对于自己的父视图不会有偏移),但是会影响它上面的子视图的位置(它的子视图相对于它会有所偏移)
    NSLog(@"bounds.x = %.2f , bounds.y = %.2f , bounds.width = %.2f , bounds.height = %.2f",redView.bounds.origin.x , redView.bounds.origin.y , redView.bounds.size.width , redView.bounds.size.height);
    

    redView.bounds = CGRectMake(10, 10, 100, 100);


    // 系统内部存放的是视图的中心点位置和大小信息,Frame方式的信息是按照中心点位置计算出来的。当我们创建一个视图的时,一般采用Frame方式。当我们旋转一个视图或者处理视图事件时,大多采用bounds方式(让其父视图的bounds改变,从而引起该视图在其父视图上的位置的改变)。

0 0
原创粉丝点击