源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

深入了解iOS开发中UIWindow的相关使用

  • 时间:2020-02-19 07:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:深入了解iOS开发中UIWindow的相关使用
UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow。 iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控制器的view添加到UIWindow上,于是控制器的view就显示在屏幕上了。 一个iOS程序之所以能显示到屏幕上,完全是因为它有UIWindow。也就说,没有UIWindow,就看不见任何UI界面。 [b]如何获取UIWindow[/b] (1)[UIApplication sharedApplication].windows  在本应用中打开的UIWindow列表,这样就可以接触应用中的任何一个UIView对象(平时输入文字弹出的键盘,就处在一个新的UIWindow中); (2)[UIApplication sharedApplication].keyWindow(获取应用程序的主窗口)用来接收键盘以及非触摸类的消息事件的UIWindow,而且程序中每个时刻只能有一个UIWindow是keyWindow; 注:经过代码验证,非keyWindow 也是可以接受键盘消息的; 提示:如果某个UIWindow内部的文本框不能输入文字,可能是因为这个UIWindow不是keyWindow; (3)view.window获得某个UIView所在的UIWindow。   [b]UIWindowLevel[/b] 我们知道UIWindow 有三个层级,分别是Normal ,StatusBar,Alert.输出他们三个层级的值,我们发现从左到右依次是0,1000,2000,也就是说Normal级别是最低的,StatusBar处于中级,Alert级别最高。而通常我们的程序的界面都是处于Normal这个级别的,系统顶部的状态栏应该是处于StatusBar级别,UIActionSheet和UIAlertView这些通常都是用来中断正常流程,提醒用户等操作,因此位于Alert级别。 根据window显示级别优先原则,级别高的会显示在最上层,级别低的在下面,我们程序正常显示的view在最底层; [b]keyWindow [/b] 官方文档中是这样解释的 “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." 翻译过来就是说,keyWindow是指定的用来接收键盘以及非触摸类的消息,而且程序中每一个时刻只能有一个window是keyWindow。   观察UIWindow的文档,我们可以发现里面有四个关于window变化的通知:   UIWindowDidBecomeVisibleNotification   UIWindowDidBecomeHiddenNotification   UIWindowDidBecomeKeyNotification   UIWindowDidResignKeyNotification   这四个通知对象中的object都代表当前已显示(隐藏),已变成keyWindow(非keyWindow)的window对象,其中的userInfo则是空的。于是我们可以注册这个四个消息,再打印信息来观察keyWindow的变化以及window的显示,隐藏的变动 变成keywindow 的流程是这样的 1.程序默认的window先显示出来 2.默认的window再变成keywindow 3.AlertView 的window显示出来 4.默认的window变成keywindow 5.最终AlertView的window变成keywindow   [b]iOS8开始UIWindow的bounds发生变化(Window本身发生了旋转)[/b]     iOS 7之前Window的bounds不会随着方向而变化,但是到了iOS 8以后,随着设备方向的旋转,window.bounds.size.width和window.bounds.size.height也会相应发生变化。     做个很简单的测试,代码如下:  
[u]复制代码[/u] 代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     // Override point for customization after application launch.         [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(orientationChanged:)                                                  name:UIDeviceOrientationDidChangeNotification                                                object:nil];         return YES; } - (void)orientationChanged:(NSNotification*)noti {         UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;     NSString *orientationDes = nil;     switch (orientation) {         case UIDeviceOrientationLandscapeLeft:             orientationDes = @"UIInterfaceOrientationLandscapeRight";             break;         case UIDeviceOrientationLandscapeRight:             orientationDes = @"UIInterfaceOrientationLandscapeLeft";             break;         case UIDeviceOrientationPortrait:             orientationDes = @"UIInterfaceOrientationPortrait";             break;         case UIDeviceOrientationPortraitUpsideDown:             orientationDes = @"UIInterfaceOrientationPortraitUpsideDown";             break;         default:             orientationDes = @"";             break;     }         NSLog(@"system ver: %@, \rorientaion: %@, \rwindow bounds: %@",           [UIDevice currentDevice].systemVersion,           orientationDes,           NSStringFromCGRect(self.window.bounds)); }
  示例代码很简单,新建一个工程,然后在delegate中直接添加以上代码即可。     iOS 8上运行结果为:
[u]复制代码[/u] 代码如下:
2014-06-04 09:26:32.016 SviOS8[4143:61114] system ver: 8.0, orientaion: UIInterfaceOrientationLandscapeRight, window bounds: {{0, 0}, {320, 480}} 2014-06-04 09:26:34.788 SviOS8[4143:61114] system ver: 8.0, orientaion: UIInterfaceOrientationPortrait, window bounds: {{0, 0}, {480, 320}} 2014-06-04 09:26:35.791 SviOS8[4143:61114] system ver: 8.0, orientaion: UIInterfaceOrientationLandscapeLeft, window bounds: {{0, 0}, {320, 480}} 2014-06-04 09:26:47.468 SviOS8[4143:61114] system ver: 8.0, orientaion: UIInterfaceOrientationPortraitUpsideDown, window bounds: {{0, 0}, {480, 320}}
  iOS 7及之前的版本运行结果为:  
[u]复制代码[/u] 代码如下:
2014-06-04 09:39:00.527 SviOS8[4380:70b] system ver: 7.0.3, orientaion: UIInterfaceOrientationLandscapeRight, window bounds: {{0, 0}, {320, 480}} 2014-06-04 09:39:00.895 SviOS8[4380:70b] system ver: 7.0.3, orientaion: UIInterfaceOrientationPortrait, window bounds: {{0, 0}, {320, 480}} 2014-06-04 09:39:01.225 SviOS8[4380:70b] system ver: 7.0.3, orientaion: UIInterfaceOrientationLandscapeLeft, window bounds: {{0, 0}, {320, 480}} 2014-06-04 09:39:11.004 SviOS8[4380:70b] system ver: 7.0.3, orientaion: UIInterfaceOrientationPortraitUpsideDown, window bounds: {{0, 0}, {320, 480}}
  通过对比我们可以清晰的看到iOS 8中UIWindow在处理旋转时策略的变更,虽然会因为与之前的版本不同导致现有项目布局存在的bug,但是可以看到iOS 8中的处理方式更加符合我们的预期,在竖向的时候我们就获取到width < height, 在横向则是 width > height,这个符合所见即所得的原则。     题外话,不管是iOS 7还是之前的版本,以及最新出的iOS 8,所有的ViewController的bounds都是正确的,所以只需要坚持一个原则“所有布局都是基于VC.view.bounds布局,那么你的App的显示就一切正常。”
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部