UI 学习 第一章 UIWindow UIview
1.UIWindow (属性)
UIWindow是一个完整的APP的容器,一个APP对应一个UIWindow:
self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];(初始化一个window,并让window的大小跟主屏幕大小相同)
self.window.backgroundColor = [UIColorredColor];(设置window背景色)
self.window.rootViewController = [[ViewControlleralloc]init];(根视图控制器)
[self.windowmakeKeyAndVisible];(让window成为主窗口)
2.UIView:
-
创建:
UIView *view = [[
UIView
alloc]
init];//创建坐标为(0,0),宽高均为0的视图
UIView *view = [[
UIView
alloc]
initWithFrame:
CGRectMake(
10,
10,
100,
50)];//创建一个相对父视图坐标为(10,10),宽100,高50的视图
-
view. frame = CGRectMake( 20, 10, 100, 50);//设置view的坐标及尺寸
-
view. userInteractionEnabled = NO;//是否响应用户交互(触摸)
-
view.tag = 1;//设置视图的标签
-
UIView *view3 = [view viewWithTag: 1];//将view子视图中标签为3的视图赋值给view3
-
-(void) addSubview:(UIView *)view;//添加子视图
[view addSubview:view2];将view2加到view上
-
-(void) removeFromSuperview;//从父窗口上删除子视图(自己)
[view
removeFromSuperview];
- [view insertSubview:view1 atIndex:0];//在view的第0层插入一个子视图
- [view insertSubview:view1 belowSubview:view2]; //将view1子视图添加到view2子视图下
- [view insertSubview:view1 aboveSubview:view2]; //将view1子视图添加到view2子视图下
- view.backgroundColor = [UIColor whiteColor];//设置背景色
- view.alpha = 1;//设置view的透明度,0~1透明到不透明
- view.hidden = YES;//是否隐藏view
Window: self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];//创建window,让其充满屏幕
[self.window makeKeyAndVisible];//让window成为主窗口且可视
self.window.backgroundColor = [UIColor whiteColor];//设置背景色
self.window.rootViewController = [[ViewController alloc] init];//设置根视图控制器
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(10, 20, 100, 100);//相对于父视图的位置,注意坐标和尺寸的合理性,保证坐标加尺寸不会超出父视图范围
// view.userInteractionEnabled = NO;//是否允许用户点击(默认YES),如果设置成no,子视图不会覆盖父视图的点击事件
view.backgroundColor = [UIColor purpleColor];
[self.view addSubview:view];//将后面的视图添加到前面的视图之上
self.view.userInteractionEnabled = NO;//如果父视图不允许交互,那么子视图的事件也会被屏蔽
view.tag =1;//设置视图的标签
view.alpha = 1;//设置视图的透明度,0~1浮点
// self.view.alpha = 0;//如果父视图透明,那么子视图也会看不见
// view.hidden = YES;//设置视图是否隐藏(默认NO)
self.view.hidden = YES;//如果父视图被隐藏,那么子视图也会被隐藏
UIView *view3 = [self.view viewWithTag:1];//获取父视图中标签为1的视图
view3.backgroundColor = [UIColor blackColor];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 100, 200)];
view.backgroundColor = [UIColor brownColor];
[self.view addSubview:view];
// [view removeFromSuperview];//将视图移除父视图
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 300)];
view1.backgroundColor = [UIColor grayColor];
[self.view addSubview:view1];
// [self.view insertSubview:view1 atIndex:3];//将子视图添加到父视图的某个位置
// [self.view insertSubview:view1 aboveSubview:view];//将view1添加到父视图,且在view之上
// [self.view insertSubview:view1 belowSubview:view];//将view1添加到父视图,且在view之下
// [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];//交换两个位置的视图
// [self.view bringSubviewToFront:view];//将某个子视图移到父视图的最前方
[self.view sendSubviewToBack:view1];//将某个子视图移到父视图的最底层