frame 和 bounds 區別

區別:

frame   是 一個控件在其父控件中的 位置尺寸spa

bounds 是一個控件以自身的座標系爲參考 獲得的  位置  尺寸 。既然以自身爲座標系,那麼 X 和 Y 在非特殊狀況下就是 0 ,0   。    寬高  仍然仍是控件自身的 寬高。code

 

特殊狀況是什麼?it

答:主動設置bounds 改變它的默認位置。class

代碼以下:object

- (void)viewDidLoad {
    [super viewDidLoad];


    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, 250)];
    //設置 view1自身的 bounds
    [view1 setBounds:CGRectMake(-20, -20, 280, 250)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];//添加到self.view
    
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view2.backgroundColor = [UIColor yellowColor];
    //添加到view1上,[此時view1座標系左上角起點爲(-20,-20)]
    [view1 addSubview:view2];
 
    

}

view1 添加在 self.view 上,    view2 添加在 view1 上。im

注意:view2的frame 設置的是 (0,0,100,100),按理說應該和view1的左上角重合,可是代碼運行起來的結果以下: (並無重合,這是由於咱們設置了view1 的自身座標系bounds)img

 

 

bounds的有如下兩個特色:

1. 它能夠修改本身座標系的原點位置,進而影想到「子view」的顯示位置。view

2. bounds,它能夠改變的frame。若是bounds比frame大。那麼frame也會跟着變大。這個做用更像邊界和大小的意思,frame至關於邊界,bounds至關於大小。vi

相關文章
相關標籤/搜索