新建
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建一个UIView对象
//所有在屏幕上看到的对象,都是它的子类
//UIView是一个矩形对象,有背景颜色,有层级关系
UIView * view = [[UIView alloc] init];
//设置UIView的位置
view.frame = CGRectMake(100, 100, 100, 200);
view.backgroundColor = [UIColor blueColor];
view.backgroundColor = [UIColor orangeColor];
//将新建试图添加到父亲视图上
//1.将新视图显示到屏幕上
//2.将视图作为父亲视图的子视图管理起来
[self.view addSubview:view];
view.hidden = YES;
//alpha = 1不透明
//alpha = 0 透明
//alpha = 0.5 半透明;
view.alpha = YES;
self.view.backgroundColor = [UIColor blueColor];
//设置是否显示不透明
view.opaque = NO;
//从父视图删除
//从父视图的管理中删除
[view removeFromSuperview];
UIView 的层级关系
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView * view01 = [[UIView alloc] init];
UIView * view02 = [[UIView alloc] init];
view01.frame = CGRectMake(100, 100, 150, 150);
view01.backgroundColor = [UIColor blueColor];
view02.frame = CGRectMake(125, 125, 150, 150);
view02.backgroundColor = [UIColor orangeColor];
UIView * view03 = [[UIView alloc] init];
view03.backgroundColor = [UIColor greenColor];
view03.frame = CGRectMake(150, 150, 150, 150);
画图顺序是添加顺序
[self.view addSubview: view01];
[self.view addSubview:view03];
[self.view addSubview:view02];
//调整视图,将view01 调整到最前面
[self.view bringSubviewToFront:view01];
//将03调整到最后面。
[self.view bringSubviewToFront:view03];
}
改变视图的俩个函数都会改变视图在subviews数组里的位置。
//subviews 管理所有self.view子视图的数组。
UIView * viewFront = self.view.subviews[2];
UIView * viewBack = self.view.subviews[0];
if (viewFront == view01) {
NSLog(@"相等");
}
同样
[view01 removeFromSuperview]也会将视图从数组中删除。