UIWindow和UIView
2015-08-30 11:38
393 查看
UIWindow和UIView
手机应用程序多是使用视窗的形式显示内容,手机应用中要在屏幕上显示内容首先要创建一个窗口承载内容,iOS应用中使用UIWindow、UIView来实现内容显示。
UIWindow:UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示、分发事件给View。UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。UIWindow初始化在appDeleDgate里面的didFinishLaunchingWithOptions
方法。
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //显示出UIWindow
UIView:UIView类继承自UIResponder,负责在屏幕上定义一个矩形区域,视图用于展示界面及响应用户界面交互。每个视图对象都要负责渲染视图区域的内容,并响应该区域中发生的操作事件。
除了显示内容和处理事件之外,视图可以嵌套并管理子视图。子视图是指嵌入到另一视图对象内部的视图对象,而被嵌入的视图对象是父视图。视图添加到window中就会显示出来,iOS中是怎么实现视图显示的,看下面的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; CGRect rect=[UIScreen mainScreen].applicationFrame; NSLog(@"NSScreen application %@",NSStringFromCGRect(rect)); CGRect rect2=[UIScreen mainScreen].bounds; NSLog(@"NSString mainScreen %@",NSStringFromCGRect(rect2)); NSLog(@"iOS_didFinishLaunchingWithOptions"); UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(60, 50, 200, 100)]; view1.backgroundColor=[UIColor greenColor]; [self.window addSubview:view1]; [view1 release]; UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(60, 100, 200, 200) ]; view2.alpha=0.5; view2.backgroundColor=[UIColor cyanColor]; [self.window addSubview:view2]; UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(100, 50, 60, 50)]; view3.backgroundColor=[UIColor colorWithRed:25/255.0 green:60/255.0 blue:150/255.0 alpha:1.0]; [view2 addSubview:view3]; [view3 release]; [view2 release]; [self.window makeKeyAndVisible]; return YES; }
上面添加了三个视图,view1、view2、view3,view3嵌套在view2中。
相关文章推荐
- UI设计规范
- iOS8新特性(2)——UIPopoverController和UIPresentationController
- iOS8新特性(1)——UIAlertController
- 编写广播时出现Exported receiver does not require permission
- 学习YUI.Ext 第一天:EXT简介(一)
- HOWTO: Install, Build and Use openCV (MacOSX 10.10)
- POJ 2524 Ubiquitous Religions(宗教种类:并差集)
- adbd cannot run as root in production builds
- iphone开发之表格组件UITableView的使用(一)使用时的具体步骤介绍
- UUID.randomUUID().toString 简单解释
- CodeForces 441A Valera and Antique Items
- iOS开发 - UICollectionViewLayout 自定义布局
- iOS开发 - UICollectionViewFlowLayout 流水布局
- UIday02~04:UITextField UIButton UIdelegate 自定义视图 容器视图控制器
- POJ 3458 && HDU 1683 Colour Sequence(水~)
- Win10 Build 10532语言包官方下载地址汇总
- POJ 3094 && HDU 2734 Quicksum(水~)
- esayyui的layout嵌套布局可以解决fit自适应的位置偏移问题
- HDU1005 - Number Sequence (矩阵快速幂)
- Arduino CNC Shiled 和 DRV8825驱动板的注意事项