·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iOS阶段学习第28天笔记(UIView的介绍)

iOS阶段学习第28天笔记(UIView的介绍)

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

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];