iOS學習(UI)知識點整理
一、關于UIVIew 的介紹
1)概念:UIView 是用于裝載并展示各類控件的大容器,是iOS中所有UI控件的基類
2)UIView 初始化實例代碼
1 UIView *view1 = [[UIView alloc] init]; 2 view1.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height/2); 3 view1.backgroundColor = [UIColor lightGrayColor]; 4 view1.tag = 1000; 5 6 //alpha屬性設置view的透明度 7 view1.alpha = 0.5; 8 9 //超出部分自動隱藏10 view1.clipsToBounds = YES;11 [self.view addSubview:view1];
3)sendSubviewToBack 父視圖可以使用sendSubviewToBack方法將子視圖放在最下層
例如:
1 UIView *view3 = [[UIView alloc] init];2 view3.frame = CGRectMake(45, 45, 240, 260);3 view3.backgroundColor = [UIColor blueColor];4 view3.tag = 1001;5 [view1 addSubview:view3];6 7 //父視圖把某一子視圖放在最下層8 [view1 sendSubviewToBack:view3];
4)bringSubviewToFront 父視圖可以使用sendSubviewToBack方法將子視圖放在最上層
例如:
1 UIView *view2 = [[UIView alloc] init];2 view2.frame = CGRectMake(40, 40, 250, 250);3 view2.backgroundColor = [UIColor blackColor];4 view2.tag = 1001;5 [view1 addSubview:view2];6 7 //父視圖把某一子視圖放在最上層8 [view1 bringSubviewToFront:view2];
5)獲取self.view中所有的子視圖 例如:
1 NSArray *subViews = self.view.subviews;
6)removeFromSuperview 將子視圖從父視圖中移除 例如:
1 [view1 removeFromSuperview];
7) viewWithTag 根據視圖Tag標記獲取視圖 例如:
1 UIView *view = [self.view viewWithTag:1000];
8) hidden 隱藏視圖 例如:
1 view.hidden = YES;
9)因為所有的UI控件都繼承自UIVIew 所以根據控件的Tag標記可以使用viewWithTag獲取控件對象
例如:
1 //1000 為button的Tag標記 注意:需要強轉一下2 UIButton *button=(UIButton*)[self.view viewWithTag:1000];
新聞熱點
疑難解答