UIView 学习知识点
UI學(xué)習(xí)第一周
UIView 視圖類 :代表屏幕上的一塊矩形區(qū)域,在屏幕上看到的任何一個(gè)元素都是UIView或者UIView的子類
創(chuàng)建UIView的幾大要素:
//1.大小 – 寬和高
//2.位置 – 視圖左上角點(diǎn)得坐標(biāo),x(橫坐標(biāo)), y(縱坐標(biāo))
//3.快速創(chuàng)建結(jié)構(gòu)體的變量的方法
CGRect -> CGREcetMake()? 包含位置和大小
CGPoint -> CGPointMake() 包含位置 x和y
CGSize -> CGSizeMake() 包含大小 width 和 height
實(shí)例:
//1,創(chuàng)建視圖對(duì)象
?UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
//2,更改背景顏色
?redView.backgroundColor = [UIColor redColor];
(在這個(gè)位置可以給視圖設(shè)置唯一標(biāo)識(shí) tag
redView.tag = 101; //可以通過(guò)tag值取到redView視圖)
//3,添加到父視圖上 -- window上
?[self.window addSubview:redView];
//4,釋放所有權(quán) -- 對(duì)應(yīng)alloc操作
? [redView release];
UIView視圖相關(guān)的屬性
?? //通過(guò)tag值獲取視圖
UIView *tempView = [self.window viewWithTag:101];
?
//打印視圖
//打印window和中心視圖的所有子視圖
NSLog(@”window:%@”, self.window.subviews);
NSLog(@”window:%@”, centerView.subviews);
//打印中間視圖和右上角視圖的父視圖
NSLog(@”center:%@”, centerView.superview);
NSLog(@”leftUp:%@”, leftUpView.superview);
//通過(guò)window的subviews獲取
UIView *tempView = [self.window.subviews objectAtIndex:1];
//視圖操作的API
//運(yùn)用創(chuàng)建視圖方法分別創(chuàng)建紅黃藍(lán)綠視圖:
? //將藍(lán)色添加到黃色視圖和紅色視圖中間
//[self.window insertSubview:blueView belowSubview:yellowView];
//[self.window insertSubview:blueView aboveSubview:redView];
//[self.window insertSubview:blueView atIndex:1];
? //該方法將視圖添加到subviews數(shù)組中最后一個(gè)位置
??? [self.window addSubview:blueView];
??? [blueView release];
? //1.把綠色視圖移動(dòng)到最后面
//[self.window sendSubviewToBack:blueView];
? //2.把綠色視圖移動(dòng)到最前面
//[self.window bringSubviewToFront:blueView];
? //3.將藍(lán)色視圖和紅色視圖換下位置
//[self.window exchangeSubviewAtIndex:3 withSubviewAtIndex:0];
? //4.將藍(lán)色視圖移除掉
//[blueView removeFromSuperview];
//管理視圖的層級(jí)關(guān)系:
?
視圖具有的和位置相關(guān)的屬性
???? 1.frame:既包含大小,也包含位置,而位置是視圖區(qū)域左上角的點(diǎn)的坐標(biāo),相對(duì)于父視圖坐標(biāo)原點(diǎn)的距離.
???? 2.center:中心點(diǎn)的位置.
???? 3(難點(diǎn)).bounds:既包含大小,也包含位置,而位置是視圖區(qū)域左上角的點(diǎn)的坐標(biāo),相對(duì)于自身視圖坐標(biāo)原點(diǎn)的距離.
改變bounds中x,y.會(huì)造成自身視圖坐標(biāo)原點(diǎn)位置發(fā)生變化,影響子視圖位置.
實(shí)例:
//布局思想 -- 創(chuàng)建一個(gè)和屏幕等大的內(nèi)容視圖,添加到window上,而所有的子視圖都添加到內(nèi)容視圖上.
??? UIView *containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
??? containerView.backgroundColor = [UIColor yellowColor];
??? [self.window addSubview:containerView];
??? [containerView release];
??? //接下來(lái)將所有的子視圖都添加到內(nèi)容視圖containerView上
轉(zhuǎn)載于:https://www.cnblogs.com/iamfoolish/p/4769807.html
總結(jié)
以上是生活随笔為你收集整理的UIView 学习知识点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [C#] 等待启动的进程执行完毕
- 下一篇: 递推DP UVA 590 Always