In my app I want to create a new UIWindow
over the main UIWindow, And I wrote as following, but it don't works. first, i create a UIWindow
as the main window, and then make it key and visible, and then create a new UIWindow
overlay, but nothing happens.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor redColor]; ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; window1.backgroundColor = [UIColor redColor]; window1.windowLevel = UIWindowLevelAlert; [window1 makeKeyAndVisible]; return YES; }
In swift a new UIWindow can be added as follows.. class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var viewController: ViewController? var navigationController: UINavigationController?
The backdrop for your app's user interface and the object that dispatches events to your views.
So think of a UIView as controlling part of the screen: drawing to it etc. A window is a mere container. The window does cover the entire screen (usually), the window is the root view in the view hierarchy (it is a UIView subclass).
UIWindow object coordinates the one or more views presenting on the screen. UIKit handles most window-related interactions, working with other objects as needed to implement many app behaviors. You use windows only when you need to do the following: Provide a main window to display your app's content.
Note that UIWindow is hidden by default, so makeKeyAndVisible both makes a UIWindow become keyWindow and set its hidden property to NO Simply add the following code in application:didFinishLaunchingWithOptions: then rotate the device/simulator, you will see that the green color view is always at position {0, 0},
Only one window at a time may be the key window. You call makeKeyAndVisible or makeKeyWindow to make a UIWindow become the keyWindow. Note that UIWindow is hidden by default, so makeKeyAndVisible both makes a UIWindow become keyWindow and set its hidden property to NO
Your window1 object is a local variable, when the code runs out of this method, this object does not exist any more. Any UIWindow object we created will be add to the [ [UIApplication sharedApplication] windows], but this array only keeps a weak reference to any UIWindow object, so it's up to your own code to keep the window object existing.
In iOS13 and higher you must create UIWindow with windowScene: initializer as follows let overlayWindow = UIWindow (windowScene: YourScene). For lower iOS versions you should use frame: initializer as follows let overlayWindow = UIWindow (frame: YourFrame).
UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; window1.backgroundColor = [UIColor redColor]; window1.windowLevel = UIWindowLevelAlert; [window1 makeKeyAndVisible];
Finally I know why it doesn't work, because window1 is a method var, and it will lost after the method executed. So I declare a new @property for it, as
@property (strong, nonatomic) UIWindow *window2;
and change the code like
UIWindow *window2 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 80, 320, 320)]; window2.backgroundColor = [UIColor redColor]; window2.windowLevel = UIWindowLevelAlert; self.window2 = window2; [window2 makeKeyAndVisible];
it works!
class ViewController: UIViewController { var coveringWindow: UIWindow? func coverEverything() { coveringWindow = UIWindow(frame: (view.window?.frame)!) if let coveringWindow = coveringWindow { coveringWindow.windowLevel = UIWindowLevelAlert + 1 coveringWindow.isHidden = false } } }
According to the documentation, to receive events that do not have a relevant coordinate value, such as keyboard entry, make it key
instead of merely !
isHidden
:
coveringWindow.makeKeyAndVisible()
You can even control the transparency of its background, for a smoke effect:
coveringWindow.backgroundColor = UIColor(white: 0, alpha: 0.5)
Note that such window needs to handle orientation changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With