#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建一个UIView对象
//UIView是iOS中的视图对象
//显示在屏幕上的所有对象的基础类
//所有显示在屏幕上的对象一定都继承于UIview
//屏幕上能看到的对象都是UIview的子类
//UIview是一个矩形对象,有背景颜色,可以显示,有层级关系
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 200);
view.backgroundColor = [UIColor blueColor];//RGB三原色
//将新建的视图添加到父亲视图上
//1.将新建的视图显示到屏幕上
//2.将视图作为父亲视图的子视图管理起来
[self.view addSubview:view];
//是否隐藏视图对象,默认值为no显示视图
view.hidden = YES;//把背景色透点
//设置视图透明度 1不透明,0透明 0.5半透明
view.alpha = 1;
//设置是否显示不透明
view.opaque = NO;
//将自己从父亲视图删除掉
//1.从父亲的管理视图删除,不会显示在屏幕上
[view removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
层级关系
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *view01 = [[UIView alloc] init];
view01.frame = CGRectMake(100, 100, 150, 150);
view01.backgroundColor = [UIColor blueColor];
UIView *view02 = [[UIView alloc] init];
view02.frame = CGRectMake(125, 125, 150, 150);
view02.backgroundColor = [UIColor orangeColor];
UIView *view03 = [[UIView alloc] init];
view03.frame = CGRectMake(150, 150, 150, 150);
view03.backgroundColor = [UIColor greenColor];
//哪一个视图先添加到父亲视图中,就先绘制哪个图,哪一个在后面
//哪一个视图被最后一个添加到视图中,就最后绘制哪个
[self.view addSubview:view01];
[self.view addSubview:view02];
[self.view addSubview:view03];
[self.view bringSubviewToFront:view01];//将某一个视图调整到最前面显示,
[self.view sendSubviewToBack:view03];//将某一个视图调整到最后面显示
//subviews管理所有self.view的子视图的数组
UIView *viewFront = self.view.subviews[2];
UIView *viewBack = self.view.subviews[0];
if(viewBack == view01){
NSLog(@"相等");
}
[view01 removeFromSuperview];
}