UIWindow并不包含任何默認(rèn)的內(nèi)容,但是它被當(dāng)作UIView的容器,用于放置應(yīng)用中所有的UIView。UIWindow更多的時(shí)候只作為UIView的頂層容器存在。
而我們最常用的方法咱枉,就是在程序剛啟動(dòng)時(shí),調(diào)用UIWindow的makeAndVisible方法徒恋,代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}```
我們可以在UIWindow做一些操作:
// 取得最上面的window self代表一個(gè)view
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
[window addSubview:self];
WindowLevel屬性
不是所有創(chuàng)建的UIWindow一定會(huì)覆蓋在界面的最上面蚕断,UIWindow有一個(gè)類型為“UIWindowLevel”的屬性,該屬性定義了UIWindow的層級(jí)入挣,系統(tǒng)定義的UIWindowLevel一共有3中取值:如下:
UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar
程序中默認(rèn)的UIWindow的層級(jí)是UIWindowLevelNormal亿乳。
手動(dòng)創(chuàng)建UIWindow
//創(chuàng)建UIWindown
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.windowLevel = UIWindowLevelNormal;
_window.backgroundColor = [UIColor blueColor];
_window.hidden = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideUIWindown)];
[_window addGestureRecognizer:tap];
/**
隱藏UIWindown
*/
- (void)hideUIWindown{
_window.hidden = YES;
_window = nil;
}
在一個(gè)應(yīng)用程序中,想做一個(gè)密碼保護(hù)功能径筏,在用戶從應(yīng)用的任何界面按Home鍵退出葛假,過(guò)一段時(shí)間再?gòu)暮笈_(tái)切換回來(lái),顯示一個(gè)密碼輸入界面滋恬。只有用戶在輸入正確的密碼聊训,才能夠進(jìn)入退出的界面。因?yàn)檫@個(gè)密碼輸入界面可能從任何應(yīng)用界面彈出恢氯,并且需要蓋在所有界面的最上層带斑,所以它很適合用一個(gè)UIWindow來(lái)實(shí)現(xiàn)。
import "PasswordInputwindow.h"
@implementation PasswordInputwindow
- (instancetype)shareInstance
{
static id shareInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareInstance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
});
return shareInstance;
}
-
(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor yellowColor];
self.rootViewController = vc;UITextField *textfield = [[UITextField alloc] init]; textfield.textColor = [UIColor blackColor]; textfield.font = [UIFont systemFontOfSize:17]; textfield.frame = CGRectMake(20, 120, [UIScreen mainScreen].bounds.size.width-40, 40); textfield.borderStyle = UITextBorderStyleRoundedRect; textfield.placeholder = @"在這里輸入密碼"; [vc.view addSubview:textfield]; UIButton *btn = [[UIButton alloc] init]; [btn setTitle:@"隱藏界面" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:17]; [btn sizeToFit]; btn.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2); [btn addTarget:self action:@selector(hideWindown:) forControlEvents:UIControlEventTouchUpInside]; [vc.view addSubview:btn];
}
return self;
} (void)hideWindown:(UIButton *)sender{
[self resignKeyWindow];
self.hidden = YES;
}(void)show
{
[self makeKeyWindow];
self.hidden = NO;
}
-
(void)applicationDidBecomeActive:(UIApplication *)application {
[[PasswordInputwindow shareInstance] show];
}
如果我們創(chuàng)建的UIWindow需要處理鍵盤事件酿雪,那么就需要合理的將其設(shè)置為keywindow遏暴。keywindow是被系統(tǒng)設(shè)計(jì)用來(lái)接受鍵盤和其他非觸摸事件的UIWindow侄刽。我們可以通過(guò)makeKeyWindow和makeKeyAndVisible方法來(lái)將自己創(chuàng)建的UIWindow實(shí)例設(shè)置為keywindow指黎。支付寶客戶端的手勢(shì)解鎖功能,就是UIWindow的一個(gè)極好的應(yīng)用州丹。
/*** 文中的知識(shí)摘自“iOS開發(fā)進(jìn)階” ****/