iOS UIKit :UIWindow
UIQWindow定义了一个window对象来管理views。一个软件只能有一个window。window的主要职能使为view提供显示取和向view传递事件。想要改变软件显示的内容,你可以改变window的root view。
UIWindow的screen属性指定了window的显示属性包括:bounds, mode, and brightness.
window notifications用来监听window 和 screen的改变,包括:
UIWindowDidBecomeVisibleNotification
UIWindowDidBecomeHiddenNotification
UIWindowDidBecomeKeyNotification
UIWindowDidResignKeyNotification
UIWindow继承自UIView,关于这一点可能有点逻辑障碍,画框怎么继承自画布呢?不要过于去专牛角尖,画框的形状不就是跟画布一样吗?拿一块画布然后用一些方法把它加强,是不是可以当一个画框用呢?这也是为什么 一个view可以直接加到另一个view上去的原因了。一个应用程序只能有一个画框。
看一下系统的初始化过程(在application didFinishLauchingWithOptions里面):
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; myViewController = [[MyViewController alloc] init]; window.rootViewController = myViewController; [window makeKeyAndVisible]; return YES; }
创建一个an external display:
- (void)checkForExistingScreenAndInitializeIfPresent { if ([[UIScreen screens] count] > 1) { // Get the screen object that represents the external display. UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1]; // Get the screen's bounds so that you can create a window of the correct size. CGRect screenBounds = secondScreen.bounds; self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds]; self.secondWindow.screen = secondScreen; // Set up initial content to display... // Show the window. self.secondWindow.hidden = NO; } } - (void)setUpScreenConnectionNotificationHandlers { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(handleScreenDidConnectNotification:) name:UIScreenDidConnectNotification object:nil]; [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:) name:UIScreenDidDisconnectNotification object:nil]; } - (void)handleScreenDidConnectNotification:(NSNotification*)aNotification { UIScreen *newScreen = [aNotification object]; CGRect screenBounds = newScreen.bounds; if (!self.secondWindow) { self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds]; self.secondWindow.screen = newScreen; // Set the initial UI for the window. } } - (void)handleScreenDidDisconnectNotification:(NSNotification*)aNotification { if (self.secondWindow) { // Hide and then delete the window. self.secondWindow.hidden = YES; self.secondWindow = nil; } }
UIWindowLevel
UIWindow有三个层级,分别是Normal,StatusBar,Alert。打印输出他们三个这三个层级的值我们发现从左到右依次是0,1000,2000,也就是说Normal级别是最低的,StatusBar处于中等水平,Alert级别最高。
通常我们的程序的界面都是处于Normal这个级别上的,系统顶部的状态栏应该是处于StatusBar级别,UIActionSheet和UIAlertView这些通常都是用来中断正常流程,提醒用户等操作,因此位于Alert级别。
KeyWindow
keyWindow是指定的用来接收键盘以及非触摸类的消息,而且程序中每一个时刻只能有一个window是keyWindow。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义