UI基础UIWindow、UIView

在PC中,应用程序多是使用视窗的形式显示内容,手机应用也不例外,手机应用中要在屏幕上显示内容首先要创建一个窗口承载内容,iOS应用中使用UIWindow、UIView来实现内容显示。

UIWindow:

       UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示、分发事件给View。UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。UIWindow初始化在appDeleDgate里面的 didFinishLaunchingWithOptions方法。

[java]  view plain copy print ?
  1. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  2. // Override point for customization after application launch.  
  3. self.window.backgroundColor = [UIColor whiteColor];  
  4. [self.window makeKeyAndVisible];//显示出UIWindow  


UIView:

       UIView类继承自UIResponder,负责在屏幕上 定义一个矩形区域,视图用于展示界面及响应用户界面交互。每个视图对象都要负责渲染视图区域的内容,并响应该区域中发生的操作事件。

       除了显示内容和处理事件之外,视图可以嵌套并管理子视图。子视图是指嵌入到另一视图对象内部的视图对象,而被嵌入的视图对象是父视图。视图添加到window中就会显示出来,iOS中是怎么实现视图显示的,看下面的代码:


[java]  view plain copy print ?
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.       
  4.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  5.     // Override point for customization after application launch.  
  6.     self.window.backgroundColor = [UIColor whiteColor];  
  7.       
  8.       
  9.     CGRect rect=[UIScreen mainScreen].applicationFrame;  
  10.       
  11.     NSLog(@"NSScreen application %@",NSStringFromCGRect(rect));  
  12.     CGRect rect2=[UIScreen mainScreen].bounds;  
  13.       
  14.     NSLog(@"NSString mainScreen %@",NSStringFromCGRect(rect2));  
  15.   
  16.     NSLog(@"iOS_didFinishLaunchingWithOptions");  
  17.       
  18.     UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(6050200100)];  
  19.     view1.backgroundColor=[UIColor greenColor];  
  20.       
  21.     [self.window addSubview:view1];  
  22.     [view1 release];  
  23.     
  24.     UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(60100200200) ];  
  25.     view2.alpha=0.5;  
  26.     view2.backgroundColor=[UIColor cyanColor];  
  27.     [self.window addSubview:view2];      
  28.      
  29.       
  30.     UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(100506050)];  
  31.     view3.backgroundColor=[UIColor colorWithRed:25/255.0 green:60/255.0 blue:150/255.0 alpha:1.0];      
  32.     [view2 addSubview:view3];  
  33.       
  34.     [view3 release];  
  35.     [view2 release];  
  36.       
  37.     [self.window makeKeyAndVisible];  
  38.     return YES;  
  39. }  

上面添加了三个视图,view1、view2、view3,view3嵌套在view2中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值