UIWindow和UIScreen讲解

UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件,但是这回影响时间的传递。

UIWindow初始化在appDeleDgate里面的 didFinishLaunchingWithOptions方法。

第一、UIWindow的作用

1.它是一个容器,用来盛放视图,它本身不做显示,是UIView视图做绘图操作。

2.它与视图控制器一起协作来呈现数据

3.它为视图和其他控制器对象的触摸起到关键性作用,分发事件给View。


第一、UIWindow的创建

iPhone应用程序通常只有一个UIWindow类的实例,该实例的创建如果是从nib文件创建,则通常有个属性变量,如果是用代码创建,则必须在创建时传入屏幕矩形,屏幕矩形可以通过UIScreen对象来取得,具体代码如下所示:

self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];

第三、常用的加载view的方法

 1、addSubview

  直接将view通过addSubview方式添加到window中,程序负责维护view的生命周期以及刷新,但是并不会为去理会view对应的ViewController,因此采用这种方法将view添加到window以后,我们还要保持view对应的ViewController的有效性,不能过早释放。

sample:

    self.switchController=[[SwithcViewController alloc] initWithNibName:@"switchView" bundle:nil];
    UIView *switchView=self.switchController.view;
    CGRect switchViewFrame=switchView.frame;
    switchViewFrame.origin.y+=[UIApplication sharedApplication].statusBarFrame.size.height;
    switchView.frame=switchViewFrame;
    [self.window addSubview:switchView];

 2、rootViewController

sample:

    self.viewController = [[[AndyViewController alloc] initWithNibName:@"AndyViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
把创建的viewcontrolle付给rootViewController,,UIWindow将会自动将其view添加到当前window中,同时负责ViewController和view的生命周期的维护,防止其过早释放.

官方解释:

rootViewController

The root view controller for the window.

@property(nonatomic, retain)UIViewController *rootViewController

Discussion

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

The default value of this property isnil.

 第四、常用属性分析:

1、windowLevel

UIWindow有三个层级,分别是Normal,StatusBar,Alert,如下所示:

UIKIT_EXTERNconstUIWindowLevel UIWindowLevelNormal;

UIKIT_EXTERNconstUIWindowLevel UIWindowLevelAlert;

UIKIT_EXTERNconstUIWindowLevel UIWindowLevelStatusBar;

  如果想改变UIWindow的层级关系,可以通过一下代码:

  self.window.windowLevel=UIWindowLevelAlert;

此时如果显示UIAlertView,我们发现他们会同时突出显示,如果self.window是normal级别,那别只有alertView会突出显示

打印输出他们三个这三个层级的值我们发现从左到右依次是0,1000,2000,也就是说Normal级别是最低的,StatusBar处于中等水平,Alert级别最高。而通常我们的程序的界面都是处于Normal这个级别上的,系统顶部的状态栏应该是处于StatusBar级别,UIActionSheet和UIAlertView这些通常都是用来中断正常流程,提醒用户等操作,因此位于Alert级别。

根据window显示级别优先的原则,级别高的会显示在上面,级别低的在下面,我们程序正常显示的view位于最底层,

官方解释:

The receiver’s window level.

@property(nonatomic)UIWindowLevel windowLevel

Discussion

Levels are ordered so that each level groups windows within it in front of those in all preceding groups. For example, alert windows appear in front of all normal-level windows. When a window enters a new level, it’s ordered in front of all its peers in that 

2.keyWindow

是唯一一个可以接受响应的Window,在一个应用程序中只有唯一一个keyWindow

通过UIApplication获得keyWindow。【UIApplication sharedApplication】.keyWindow,如果视图还没有渲染出来,去keyWindow是失败的。

官方解释:

The key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window.

常用一下四个方法来操作

  • – makeKeyAndVisible
  • – becomeKeyWindow
  • – makeKeyWindow
  • – resignKeyWindow

UIScreen

可以说是IOS物理设备的替代者,【【UIScreen mainScreen】bounds]获取设备大小,如果iphone和iPad的尺寸不一样,如果做适配要用该属性。

常用的属性:

Screen size:屏幕尺寸

Resolution:屏幕分变率

iphone4之前设备的分辨率 320*480

iphone4分辨率是640*960

iphone5分辨率是640*1136

iPad、iPad2:1024*768

ipad3、iPad4:2048*1536

ipadmini:1024*768

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值