您的位置:首页 > 产品设计 > UI/UE

UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口

2017-10-29 11:27 567 查看
UIWindow的常用方法:

- (void)makeKeyWindow;
让当前UIWindow变成keyWindow(主窗口)

- (void)makeKeyAndVisible; 
让当前UIWindow变成keyWindow,并显示出来

UIWindow的获得:
[UIApplicationsharedApplication].windows
获取当前应用的所有的UIWindow

[UIApplicationsharedApplication].keyWindow
获取当前应用的主窗口
view.window
获得某个UIView所在的UIWindow

下面是一个demo,用来测试UIWindow:
1,新建一个没有main.toryboard的工程


2,在delegate的didFinishLaunchingWithOptions:方法中创建UIWindow:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
////  JLAppDelegate.h//  01-UIWindow////  Created by XinYou on 15-3-11.//  Copyright (c) 2015ĺš´ vxinyou. All rights reserved.//
#import <UIKit/UIKit.h>
@interface JLAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *window2;
@end

来自CODE的代码片
JLAppDelegate.h

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
////  JLAppDelegate.m//  01-UIWindow////  Created by XinYou on 15-3-11.//  Copyright (c) 2015年 vxinyou. All rights reserved.//
#import "JLAppDelegate.h"
@implementation JLAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // 第1个window self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; self.window.backgroundColor = [UIColor redColor]; // 让window成为keyWindow(主窗口),默认不可见// [self.window makeKeyWindow]; // 让window成为keyWindow(主窗口),并且可见 [self.window makeKeyAndVisible]; // 给第1个window添加一个输入框 UITextField *tf = [[UITextField alloc] init]; tf.frame = CGRectMake(10, 10, 100, 30); tf.borderStyle = UITextBorderStyleRoundedRect; [self.window addSubview:tf]; // 打印当前应用的主窗口,此时应用的主窗口是window NSLog(@"%p", [UIApplication sharedApplication].keyWindow); // 第2个window self.window2 = [[UIWindow alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; self.window2.backgroundColor = [UIColor blueColor]; // 让window2成为keyWindow(主窗口),并且可见。此时window将不再是主窗口,但是仍然能显示 [self.window2 makeKeyAndVisible]; // 给第2个window添加一个输入框 UITextField *tf2 = [[UITextField alloc] init]; tf2.frame = CGRectMake(50, 50, 70, 40); tf2.borderStyle = UITextBorderStyleRoundedRect; [self.window2 addSubview:tf2]; // 打印当前应用的主窗口,此时window2已经成为了应用的主窗口 NSLog(@"%p", [UIApplication sharedApplication].keyWindow); return YES;}
- (void)applicationWillResignActive:(UIApplication *)application{ // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}
- (void)applicationDidEnterBackground:(UIApplication *)application{ // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}
- (void)applicationWillEnterForeground:(UIApplication *)application{ // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}
- (void)applicationDidBecomeActive:(UIApplication *)application{ // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}
- (void)applicationWillTerminate:(UIApplication *)application{ // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}
@end

来自CODE的代码片
JLAppDelegate.m

3,效果图:



3.1,从代码中我们可以得知,红色部分的window不是主窗口,蓝色部分的window是主窗口。

3.2,在ios7.1的模拟器中,主窗口和非主窗口中的输入框都能输入文字,但是在ios6.1的模拟器中,非主窗口的输入框不能输入文字。

注意:如果有时候发现文本输入框不能输入文字,那就有可能是因为该文本输入框所处的UIWindow不是keyWindow(主窗口)

3.3,弹出的键盘处在一个新的UIWindow中,也就是说键盘弹出后,这个程序就有了3个UIWindow。

如何证实键盘处在一个新的UIWindow中呢?在弹出键盘之前,通过[UIApplication sharedApplication].windows方法获取到所有的UIWindow,我们可以在主窗口中弄一个按钮,并给按钮设置监听(假设监听方法是btnClick方法),弹出键盘之后,在btnClick方法中通过[UIApplication sharedApplication].windows方法获取到所有的UIWindow,比较之前获取到的所有的UIWindow就可以证明了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: