Do you really know about UIWindow ?, UIWindow
I. First, let's take a look at the definition of UIWindow.
NS_CLASS_AVAILABLE_IOS (2_0) @ interface UIWindow: UIView // window screen. The default value is [UIScreen mainScreen], which cannot be changed; otherwise, no interface @ property (nonatomic, strong) UIScreen * screen NS_AVAILABLE_IOS (3_2); // view level of the window. The default value is 0.0 @ property (nonatomic) UIWindowLevel windowLevel; // whether it is keyWindow @ property (nonatomic, readonly, getter = isKeyWindow) BOOL keyWindow; // This method should not be manually called. When the window changes to keyWindow, it will be automatically called to notify the window. You can inherit UIWindow to override this method to implement the function-(void) becomeKeyWindow; // This method should not be manually called, when a window is no longer a keyWindow (for example, the-makeKeyWindow or-makeKeyAndVisible method is called by other window instances), it is automatically called to notify the window. This method can be inherited from UIWindow to implement functions. -(Void) resignKeyWindow; // This method changes the window to a keyWindow, but does not affect the display status-(void) makeKeyWindow; // display the main window and set the window to a key, this method enables the window to be displayed and changed to the keyWindow. By calling this method, the window is placed at the top of its level group. If you only want to change the display of the window without affecting the keyWindow status, you can directly set the hidden attribute of the window to NO. -(Void) makeKeyAndVisible; // root controller @ property (nullable, nonatomic, strong) UIViewController * rootViewController NS_AVAILABLE_IOS (4_0); // UIApplication calls the window method to distribute events to the window, window then distributes events to appropriate targets, such as distributing touch events to Real-touch views. You can directly call this method to distribute custom events. -(Void) sendEvent :( UIEvent *) event; // convert a coordinate in the window to the coordinate value (CGPoint) convertPoint (CGPoint) in the target window) point toWindow :( nullable UIWindow *) window; // convert a coordinate in the target window to the coordinate value in the window-(CGPoint) convertPoint :( CGPoint) point fromWindow :( nullable UIWindow *) window; // convert a matrix in the window to the matrix value in the target window-(CGRect) convertRect :( CGRect) rect toWindow :( nullable UIWindow *) window; // convert a matrix in the target window to the matrix value in the window-(CGRect) convertRect :( CGRect) rect fromWindow :( nullable UIWindow *) window; @ end
UIWindow is a special type of UIView. Usually there is at least one UIWindow in an app. After the iOS program is started, the first View control created is UIWindow, the View of the controller is created, and the View of the controller is added to the UIWindow, the Controller View is displayed on the screen. The reason why an iOS program can be displayed on the screen is that it has a UIWindow, that is, there is no UI interface without a UIWindow. The status bar and keyboard are both special uiwindows. UIWindow is also an important part of event processing. When an application initially receives an event, it will distribute it to a suitable UIWindow object, then it is passed to the appropriate UIView object. At the same time, window and view controller objects work together to achieve interface rotation and other control operations.
Knowledge Point 1: set the level of UIWindow with the UIWindowLevel Constant Value
UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal; // default value: Internal const UIWindowLevel UIWindowLevelAlert; // value: 2000 UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar; // value: 1000
This attribute indicates the position of the window in the Z axis of the screen. The system provides three constant values of UIWindowLevel. The main window of the application content is set to UIWindowLevelNormal by default, that is, the lowest level. Other interface elements, such as the status bar and alert view, use the corresponding level. WindowLevel can also be set to any other value, such as the window where the system keyboard is located. Its windowLevel is always greater than the level 1 of the window that calls the keyboard. Copy the input content, the windowLevel of the window where options such as pasting are located is 2100. You can set multiple windows to the same windowLevel. In these windows, the window that calls the display method is listed above. In the window groups of different levels, a window with a higher level is always above a window with a lower level.
Knowledge Point 2: Monitoring window notifications:
UIKIT_EXTERN NSString * const identifier; // triggered when the window is activated and displayed on the interface. UIKIT_EXTERN NSString * const UIWindowDidBecomeHiddenNotification is returned. // triggered when the window is hidden, no actual test is performed. null UIKIT_EXTERN NSString * const UIWindowDidBecomeKeyNotification is returned. // triggered when the window is set to keyWindow, null UIKIT_EXTERN NSString * const UIWindowDidResignKeyNotification is returned; // triggered when the key position of the window is replaced and null is returned
Knowledge Point 3: code to create the window initialization Interface
- (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;}
Knowledge Point 4: How does UIWindow be created in a storyboard project?
When you click the Application icon, run the Main function, run UIApplicationMain (), create an Application based on the third and fourth parameters, and create a proxy, and set the proxy to application (view the project configuration file info. the name of the storyboard in plist, locate the corresponding storyboard Based on the name), and enable an event loop. After the program is loaded, it will call the proxy's didfinishlaunchingwitexceptions: method. Before calling the didfinishlaunchingwitexceptions: method, storyboard is loaded, and a window is created during loading. Next, a controller pointing to the arrow is created, and the Controller is set as the root controller of UIWindow, next, display the window, that is, the page displayed after running.
Point 5: Get UIwindow
1: [UIApplication sharedApplication]. the UIWindow list opened by windows in this application, so that you can access any UIView object in the application (the keyboard popped up by the input text is located in a new UIWindow ).
2: [UIApplication sharedApplication]. keyWindow (obtain the main window of the Application) is the UIWindow used to receive keyboard and non-touch message events. In the program, only one UIWindow is a keyWindow at a time.
Tip: If a text box in a UIWindow cannot be entered, it may be because this UIWindow is not a keyWindow.
3: view. window: Obtain the UIWindow of a UIView.