ios--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的確存在。

復(fù)制代碼代碼如下:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert View" message:@"Hello Wolrd, i'm AlertView!!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];

[alertView show];

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Don't do that!" otherButtonTitles:@"Hello Wolrd", nil];

[actionSheet showInView:self.view];

下面是程序運(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的定義:

復(fù)制代碼代碼如下:

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的值分別如下:

52012-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è)試代碼:

復(fù)制代碼代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

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)。代碼如下:

復(fù)制代碼代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

self.viewController = [[SvUIWindowViewController alloc] initWithNibName:@"SvUIWindowViewController" bundle:nil];

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;

}

復(fù)制代碼代碼如下:

- (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)利”。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • UIView的功能 負(fù)責(zé)渲染區(qū)域的內(nèi)容,并且響應(yīng)該區(qū)域內(nèi)發(fā)生的觸摸事件 UIWindow 在iOS App中,UI...
    小蘑菇2閱讀 832評(píng)論 4 5
  • UIWindow *window = [[UIApplication sharedApplication]wind...
    小劉_假裝是個(gè)程序員閱讀 4,364評(píng)論 0 2
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,751評(píng)論 1 14
  • keyWindow 在我們通過(guò)模板簡(jiǎn)歷工程的時(shí)候,xcode會(huì)自動(dòng)幫我們生成一個(gè)window,然后讓它變成keyW...
    曉飛90閱讀 2,508評(píng)論 0 1
  • 一、問(wèn)題背景 最近需求量放緩,想起了以前曾經(jīng)later的小需求,也就是彈出來(lái)的AlertView中間的文本框輸入一...
    唐笛_Dylan閱讀 18,839評(píng)論 6 29

友情鏈接更多精彩內(nèi)容