1 創(chuàng)建UIView對(duì)象
UIView是iOS中的視圖對(duì)象
顯示在屏幕上的所有對(duì)象的基礎(chǔ)類
所有顯示在屏幕上的類都繼承UIView,它是所有控件的父類
是一個(gè)矩形對(duì)象,有背景,可以顯示,有層級(jí)關(guān)系
UIView *view = [[UIView alloc] init];
//設(shè)置UIView的位置
view.frame = CGRectMake(100, 100, 100, 200);
view.backgroundColor = [UIColor yellowColor];
//將新建的視圖添加到父親視圖上
//1,將新建的視圖顯示到屏幕上
//2,將視圖作為父親視圖的子類管理
[self.view addSubview:view];
2 UIView的特殊屬性
//是否隱藏視圖對(duì)象,默認(rèn)NO,顯示
view.hidden=YES;
//設(shè)置視圖的透明度
//1不透明,到0漸變?yōu)橥该?view.alpha=0.9;
self.view.backgroundColor = [UIColor blueColor];
//設(shè)置是否顯示不透明,默認(rèn)初始值是YES(不透明)
view.opaque = NO;
//將自己從父親視圖移除掉
//1,從父親視圖的管理中刪除
//2,不會(huì)顯示在屏幕
[view removeFromSuperview];
ios開(kāi)發(fā)之View屬性hidden, opaque, alpha的區(qū)別
You should always set the value of this property to NO if the view is fully or partially transparent.
An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable.
3 UIView的層級(jí)管理
- addSubview
- bringSubviewToFront
- sendSubviewToBack
- self.view.subviews[0]
- view.superview
//將三個(gè)視圖顯示到屏幕上,并添加到父親視圖里
//哪一個(gè)視圖先添加到父親視圖上,就先繪制哪一個(gè)視圖
//哪一個(gè)視圖最后一個(gè)添加到父親視圖中,就最后繪制
[self.view addSubview:view01];
[self.view addSubview:view02];
[self.view addSubview:view03];
//將某一個(gè)視圖調(diào)整到最前面顯示
//參數(shù)就是一個(gè)UIView對(duì)象,調(diào)整哪一個(gè)視圖到最前面
[self.view bringSubviewToFront:view02];
//將某一個(gè)視圖調(diào)整到最后顯示
[self.view sendSubviewToBack:view02];
//subviews是管理所有self.view的子視圖的數(shù)組
UIView *viewFront = self.view.subviews[2];
UIView *viewBack = self.view.subviews[0];
if(viewBack == view02){
NSLog(@"same");
}
//每個(gè)視圖只有一個(gè)父親視圖,而有多個(gè)子視圖
//view01.superview;