詳解iOS設(shè)計(jì)中的UIWindow使用
每一個(gè)IOS程序都有一個(gè)UIWindow,在我們通過(guò)模板簡(jiǎn)歷工程的時(shí)候,xcode會(huì)自動(dòng)幫我們生成一個(gè)window,然后讓它變成keyWindow并顯示出來(lái)。這一切都來(lái)的那么自然,以至于我們大部分時(shí)候都忽略了自己也是可以創(chuàng)建UIWindow對(duì)象。
通常在我們需要自定義UIAlertView的時(shí)候(IOS 5.0以前AlertView的背景樣式等都不能換)我們可以使用UIWindow來(lái)實(shí)現(xiàn)(設(shè)置windowLevel為Alert級(jí)別),網(wǎng)上有很多例子,這里就不詳細(xì)說(shuō)了。
一、UIWindowLevel
我們都知道UIWindow有三個(gè)層級(jí),分別是Normal,StatusBar,Alert。打印輸出他們?nèi)齻€(gè)這三個(gè)層級(jí)的值我們發(fā)現(xiàn)從左到右依次是0,1000,2000,也就是說(shuō)Normal級(jí)別是最低的,StatusBar處于中等水平,Alert級(jí)別最高。而通常我們的程序的界面都是處于Normal這個(gè)級(jí)別上的,系統(tǒng)頂部的狀態(tài)欄應(yīng)該是處于StatusBar級(jí)別,UIActionSheet和UIAlertView這些通常都是用來(lái)中斷正常流程,提醒用戶等操作,因此位于Alert級(jí)別。
上一篇文章中我也提到了一個(gè)猜想,既然三個(gè)級(jí)別的值之間相差1000,而且我們細(xì)心的話查看UIWindow的頭文件就會(huì)發(fā)現(xiàn)有一個(gè)實(shí)例變量_windowSublevel,那我們就可以定義很多中間級(jí)別的Window。例如可以自定義比系統(tǒng)UIAlertView級(jí)別低一點(diǎn)兒的window。于是寫了一個(gè)小demo,通過(guò)打印發(fā)現(xiàn)系統(tǒng)的UIAlertView的級(jí)別是1996,而與此同時(shí)UIActionSheet的級(jí)別是2001,這樣也驗(yàn)證了subLevel的確存在。
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert View"
message:@"Hello Wolrd, i'm AlertView!!!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel", nil];
[alertView show];
[alertView release];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet"
delegate:nil
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Don't do that!"
otherButtonTitles:@"Hello Wolrd", nil];
[actionSheet showInView:self.view];
[actionSheet release];
下面是程序運(yùn)行截圖:

根據(jù)window顯示級(jí)別優(yōu)先的原則,級(jí)別高的會(huì)顯示在上面,級(jí)別低的在下面,我們程序正常顯示的view位于最底層,至于具體怎樣獲取UIAlertView和UIActionSheet的level,我會(huì)在下面第二部分keyWindow中介紹并給出相應(yīng)的代碼。
UIWindow在顯示的時(shí)候會(huì)根據(jù)UIWindowLevel進(jìn)行排序的,即Level高的將排在所有Level比他低的層級(jí)的前面。下面我們來(lái)看UIWindowLevel的定義:
const UIWindowLevel UIWindowLevelNormal;
const UIWindowLevel UIWindowLevelAlert;
const UIWindowLevel UIWindowLevelStatusBar;
typedef CGFloat UIWindowLevel;
IOS系統(tǒng)中定義了三個(gè)window層級(jí),其中每一個(gè)層級(jí)又可以分好多子層級(jí)(從UIWindow的頭文件中可以看到成員變量CGFloat _windowSublevel;),不過(guò)系統(tǒng)并沒(méi)有把則個(gè)屬性開(kāi)出來(lái)。UIWindow的默認(rèn)級(jí)別是UIWindowLevelNormal,我們打印輸出這三個(gè)level的值分別如下:
2012-03-27 22:46:08.752 UIViewSample[395:f803] Normal window level: 0.000000 2012-03-27 22:46:08.754 UIViewSample[395:f803] Alert window level: 2000.000000 2012-03-27 22:46:08.755 UIViewSample[395:f803] Status window level: 1000.000000
這樣印證了他們級(jí)別的高低順序從小到大為Normal < StatusBar < Alert,下面請(qǐng)看小的測(cè)試代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor yellowColor];
[self.window makeKeyAndVisible];
UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
normalWindow.backgroundColor = [UIColor blueColor];
normalWindow.windowLevel = UIWindowLevelNormal;
[normalWindow makeKeyAndVisible];
CGRect windowRect = CGRectMake(50,
50,
[[UIScreen mainScreen] bounds].size.width - 100,
[[UIScreen mainScreen] bounds].size.height - 100);
UIWindow *alertLevelWindow = [[UIWindow alloc] initWithFrame:windowRect];
alertLevelWindow.windowLevel = UIWindowLevelAlert;
alertLevelWindow.backgroundColor = [UIColor redColor];
[alertLevelWindow makeKeyAndVisible];
UIWindow *statusLevelWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
statusLevelWindow.windowLevel = UIWindowLevelStatusBar;
statusLevelWindow.backgroundColor = [UIColor blackColor];
[statusLevelWindow makeKeyAndVisible];
NSLog(@"Normal window level: %f", UIWindowLevelNormal);
NSLog(@"Alert window level: %f", UIWindowLevelAlert);
NSLog(@"Status window level: %f", UIWindowLevelStatusBar);
return YES;
}
運(yùn)行結(jié)果如下圖:

我們可以注意到兩點(diǎn):
1)我們生成的normalWindow雖然是在第一個(gè)默認(rèn)的window之后調(diào)用makeKeyAndVisible,但是仍然沒(méi)有顯示出來(lái)。這說(shuō)明當(dāng)Level層級(jí)相同的時(shí)候,只有第一個(gè)設(shè)置為KeyWindow的顯示出來(lái),后面同級(jí)的再設(shè)置KeyWindow也不會(huì)顯示。
2)statusLevelWindow在alertLevelWindow之后調(diào)用makeKeyAndVisible,仍然只是顯示在alertLevelWindow的下方。這說(shuō)明UIWindow在顯示的時(shí)候是不管KeyWindow是誰(shuí),都是Level優(yōu)先的,即Level最高的始終顯示在最前面。
二、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." 翻譯過(guò)來(lái)就是說(shuō),keyWindow是指定的用來(lái)接收鍵盤以及非觸摸類的消息,而且程序中每一個(gè)時(shí)刻只能有一個(gè)window是keyWindow。
下面我們寫個(gè)簡(jiǎn)單的例子看看非keyWindow能不能接受鍵盤消息和觸摸消息,程序中我們?cè)趘iew中添加一個(gè)UITextField,然后新建一個(gè)alert級(jí)別的window,然后通過(guò)makeKeyAndVisible讓它變成keyWindow并顯示出來(lái)。代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[SvUIWindowViewController alloc] initWithNibName:@"SvUIWindowViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 80, 320, 320)];
window1.backgroundColor = [UIColor redColor];
window1.windowLevel = UIWindowLevelAlert;
[window1 makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self registerObserver];
// add a textfield
UITextField *filed = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
filed.placeholder = @"Input something here";
filed.clearsOnBeginEditing = YES;
filed.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:filed];
[filed release];
}
運(yùn)行截圖如下:
從圖中可以看出,雖然我們自己新建了一個(gè)然后設(shè)置為keyWindow并顯示,但是點(diǎn)擊程序中默認(rèn)window上添加的textField還是可以喚出鍵盤,而且還可以正常接受鍵盤輸入,只是鍵盤被擋住了,說(shuō)明非keyWindow也是可以接受鍵盤消息,這一點(diǎn)和文檔上說(shuō)的不太一樣。
觀察UIWindow的文檔,我們可以發(fā)現(xiàn)里面有四個(gè)關(guān)于window變化的通知:
UIWindowDidBecomeVisibleNotification UIWindowDidBecomeHiddenNotification UIWindowDidBecomeKeyNotification UIWindowDidResignKeyNotification
這四個(gè)通知對(duì)象中的object都代表當(dāng)前已顯示(隱藏),已變成keyWindow(非keyWindow)的window對(duì)象,其中的userInfo則是空的。于是我們可以注冊(cè)這個(gè)四個(gè)消息,再打印信息來(lái)觀察keyWindow的變化以及window的顯示,隱藏的變動(dòng)。
代碼如下:
根據(jù)打印的信息我們可以看出流程如下:
1、程序默認(rèn)的window先顯示出來(lái)
2、默認(rèn)的window再變成keyWindow
3、AlertView的window顯示出來(lái)
4、默認(rèn)的window變成非keyWindow
5、最終AlertView的window變成keyWindow
總體來(lái)說(shuō)就是“要想當(dāng)老大(keyWindow),先從小弟(非keyWindow)開(kāi)始混起” 而且根據(jù)打印的信息我們同事可以知道默認(rèn)的window的level是0,即normal級(jí)別;AlertView的window的level是1996,比Alert級(jí)別稍微低了一點(diǎn)兒。
b、當(dāng)我們打開(kāi)viewDidAppear中“[self presentActionSheet];”的時(shí)候,控制臺(tái)輸出如下:
keyWindow的變化和window的顯示和上面的流程一樣,同時(shí)我們可以看出ActionSheet的window的level是2001。
c、接著上一步,我們點(diǎn)擊彈出ActionSheet的cancel的時(shí)候,控制臺(tái)輸出如下:
我們看出流程如下:
1、首先ActionSheet的window變成非keyWindow
2、程序默認(rèn)的window變成keyWindow
3、ActionSheet的window在隱藏掉
總體就是“想隱居幕后可以,但得先交出權(quán)利”。
- WMI獲取硬件信息封裝函數(shù)方法(聯(lián)想臺(tái)式機(jī)出廠編號(hào) CPUID BIOS序列號(hào) 硬盤信息 顯卡信息 MAC地址)
- IOS開(kāi)發(fā)代碼分享之設(shè)置UISearchBar的背景顏色
- IOS中UIWebView加載Loading的實(shí)現(xiàn)方法
- iOS開(kāi)發(fā)之使用Storyboard預(yù)覽UI在不同屏幕上的運(yùn)行效果
- iOS - UIButton(UIEdgeInsets)/設(shè)置button上的文字和圖片上下垂直居中對(duì)齊
- iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController
- iOS開(kāi)發(fā)中UIDatePicker控件的使用方法簡(jiǎn)介
- iOS開(kāi)發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
- IOS UI學(xué)習(xí)教程之設(shè)置UITextField各種屬性
相關(guān)文章
IOS 代理方式實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了IOS 代理方式實(shí)現(xiàn)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11iOS實(shí)現(xiàn)漸變按鈕Gradient Button的方法示例
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)漸變按鈕Gradient Button的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08iOS實(shí)現(xiàn)相冊(cè)多選圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)相冊(cè)多選圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08IOS網(wǎng)絡(luò)請(qǐng)求之AFNetWorking 3.x 使用詳情
本篇文章主要介紹了IOS網(wǎng)絡(luò)請(qǐng)求之AFNetWorking 3.x 使用詳情,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController
UIActivityViewController類是一個(gè)標(biāo)準(zhǔn)的view controller,通個(gè)使用這個(gè)controller,你的應(yīng)用程序就可以提供各種服務(wù)。本文給大家介紹iOS9 系統(tǒng)分享調(diào)用之UIActivityViewController,感興趣的朋友一起學(xué)習(xí)吧2015-11-11