Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To create a new UIWindow over the main window

Tags:

ios

uiwindow

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; } 
like image 994
bohan Avatar asked Nov 15 '13 07:11

bohan


People also ask

How do I create a new UIWindow?

In swift a new UIWindow can be added as follows.. class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var viewController: ViewController? var navigationController: UINavigationController?

What is a UIWindow?

The backdrop for your app's user interface and the object that dispatches events to your views.

What is the difference between UIWindow and UIView?

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).

What is the purpose of UIWindow object?

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.

How to make a UIWindow become a keywindow?

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},

How to make a window the key window of a window?

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

What is the use of your window1 object in UIApplication?

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.

How to create an overlay window in iOS 13?

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).


2 Answers

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!

like image 141
bohan Avatar answered Oct 05 '22 09:10

bohan


Xcode 8 + Swift

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.

like image 30
SwiftArchitect Avatar answered Oct 05 '22 11:10

SwiftArchitect