UIWindow

IOS中使用 UIView类来表示窗口,一个应用程序只创建一个 UIWindow 。先创建 UIWindow 在创建控制器的view,然后再将控制器的view添加到 UIWindow 上。
 
一、AppDelegate.m
- (BOOL)方法
1、系统创建window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];//创建window,让其充满屏幕
2、设置为主窗口并显示出来
    [self.window makeKeyAndVisible];//让window成为主窗口且可视
3、系统设置创建window的背景颜色
    self.window.backgroundColor = [UIColor whiteColor];//设置背景色
4、设置根视图控制器
    self.window.rootViewController = [[ViewController alloc] init];//设置根视图控制器
 
注意:应用程序启动后,先创建Application,再创建它的代理,之后创建 UIWindow。UIWindow 继承自UIView 。
 
二、ViewController.m  
- (void)viewDidLoad 方法
1、初始化UIView对象
UIView *view = [[UIView alloc] init];//初始化一个UIview对象
2、设置UIView对象的坐标
view.frame = CGRectMake(10, 20, 100, 100);//相对于父视图的位置,注意坐标和尺寸的合理性,保证坐标加尺寸不会超出父视图范围
3、设置对象的背景颜色
view.backgroundColor = [UIColor purpleColor];
4、把设置的UIView对象添加到 self.view
[self.view addSubview:view];//将后面的视图添加到前面的视图之上
5、允许父视图交互
self.view.userInteractionEnabled = YES;//如果父视图不允许交互,那么子视图的事件也会被屏蔽
6、可设置允许用户点击
view.userInteractionEnabled = NO;//是否允许用户点击(默认YES),如果设置成no,子视图不会覆盖父视图的点击事件
点击的实现函数
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"被点击");
}
 
三、UIview的基本属性
1、tag 标签属性(给视图添加标记,被标记的视图可以使用viewWithTag方法取出)
view.tag =1;//设置视图的标签
UIView *view3 = [self.view viewWithTag:1];//获取父视图中标签为1的视图
这里可以为其设置颜色
view3.backgroundColor = [UIColor yellowColor];
2、alpha(控制视图的透明度)
view.alpha = 1;//设置视图的透明度,0~1浮点
0--完全透明,完全看不到;1--不透明,可以看的到。
self.view.alpha = 0;//如果父视图透明,那么子视图也会看不见
3、hidden(控制视图隐藏)
view.hidden = YES;//设置视图是否隐藏(默认NO不隐藏)
self.view.hidden = YES;//如果父视图被隐藏,那么子视图也会被隐藏
 
四、UIview的基本方法
先初始化两个UIview对象
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 100, 200)];
    view.backgroundColor = [UIColor brownColor];
    [self.view addSubview:view];
 
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 300)];
    view1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view1];
1、 [view removeFromSuperview];//将视图移除父视图
2、 [self.view insertSubview:view1 atIndex:3];//将子视图添加到父视图的某个位置
3、 [self.view insertSubview:view1 aboveSubview:view];//将view1添加到父视图,且在view之上
4、 [self.view insertSubview:view1 belowSubview:view];//将view1添加到父视图,且在view之下
5、 [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];//交换两个位置的视图
6、 [self.view bringSubviewToFront:view];//将某个子视图移到父视图的最前方
7、 [self.view sendSubviewToBack:view1];//将某个子视图移到父视图的最底层
 
 
 
 
原文地址:https://www.cnblogs.com/wxzboke/p/4950324.html