深入了解iOS開發(fā)中UIWindow的相關(guān)使用
UIWindow是一種特殊的UIView,通常在一個app中只會有一個UIWindow。
iOS程序啟動完畢后,創(chuàng)建的第一個視圖控件就是UIWindow,接著創(chuàng)建控制器的view,最后將控制器的view添加到UIWindow上,于是控制器的view就顯示在屏幕上了。
一個iOS程序之所以能顯示到屏幕上,完全是因為它有UIWindow。也就說,沒有UIWindow,就看不見任何UI界面。
如何獲取UIWindow
(1)[UIApplication sharedApplication].windows 在本應(yīng)用中打開的UIWindow列表,這樣就可以接觸應(yīng)用中的任何一個UIView對象(平時輸入文字彈出的鍵盤,就處在一個新的UIWindow中);
(2)[UIApplication sharedApplication].keyWindow(獲取應(yīng)用程序的主窗口)用來接收鍵盤以及非觸摸類的消息事件的UIWindow,而且程序中每個時刻只能有一個UIWindow是keyWindow;
注:經(jīng)過代碼驗證,非keyWindow 也是可以接受鍵盤消息的;
提示:如果某個UIWindow內(nèi)部的文本框不能輸入文字,可能是因為這個UIWindow不是keyWindow;
(3)view.window獲得某個UIView所在的UIWindow。
UIWindowLevel
我們知道UIWindow 有三個層級,分別是Normal ,StatusBar,Alert.輸出他們?nèi)齻€層級的值,我們發(fā)現(xiàn)從左到右依次是0,1000,2000,也就是說Normal級別是最低的,StatusBar處于中級,Alert級別最高。而通常我們的程序的界面都是處于Normal這個級別的,系統(tǒng)頂部的狀態(tài)欄應(yīng)該是處于StatusBar級別,UIActionSheet和UIAlertView這些通常都是用來中斷正常流程,提醒用戶等操作,因此位于Alert級別。
根據(jù)window顯示級別優(yōu)先原則,級別高的會顯示在最上層,級別低的在下面,我們程序正常顯示的view在最底層;
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." 翻譯過來就是說,keyWindow是指定的用來接收鍵盤以及非觸摸類的消息,而且程序中每一個時刻只能有一個window是keyWindow。
觀察UIWindow的文檔,我們可以發(fā)現(xiàn)里面有四個關(guān)于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
iOS8開始UIWindow的bounds發(fā)生變化(Window本身發(fā)生了旋轉(zhuǎn))
iOS 7之前Window的bounds不會隨著方向而變化,但是到了iOS 8以后,隨著設(shè)備方向的旋轉(zhuǎn),window.bounds.size.width和window.bounds.size.height也會相應(yīng)發(fā)生變化。
做個很簡單的測試,代碼如下:
- (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上運行結(jié)果為:
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及之前的版本運行結(jié)果為:
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在處理旋轉(zhuǎn)時策略的變更,雖然會因為與之前的版本不同導(dǎo)致現(xiàn)有項目布局存在的bug,但是可以看到iOS 8中的處理方式更加符合我們的預(yù)期,在豎向的時候我們就獲取到width < height, 在橫向則是 width > height,這個符合所見即所得的原則。
題外話,不管是iOS 7還是之前的版本,以及最新出的iOS 8,所有的ViewController的bounds都是正確的,所以只需要堅持一個原則“所有布局都是基于VC.view.bounds布局,那么你的App的顯示就一切正常?!?br />
相關(guān)文章
Objective-C實現(xiàn)冒泡排序算法的簡單示例
冒泡排序即是依次比較相鄰的兩個數(shù),如果后面的數(shù)較小則交換到前面一個數(shù)的位置上,這里我們來看一下Objective-C實現(xiàn)冒泡排序算法的簡單示例2016-06-06iOS 條碼及二維碼掃描(從相冊中讀取條形碼/二維碼)及掃碼過程中遇到的坑
本文主要給大家介紹ios中從手機相冊中讀取條形碼和二維碼的問題及解決辦法,需要的朋友參考下2017-01-01iOS App設(shè)計模式開發(fā)中對迭代器模式的使用示例
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對迭代器模式的使用示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS封裝倒計時按鈕HLCountDownButton示例詳解
這篇文章主要為大家介紹了iOS封裝倒計時按鈕HLCountDownButton示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫
這篇文章主要介紹了iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫,SQLite是一個簡單的嵌入式數(shù)據(jù)庫,非常適合輕量級使用,需要的朋友可以參考下2015-11-11