To Create a New Uiwindow Over the Main Window

To create a new UIWindow over the main window

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!

UIWindow not showing over content in iOS 13

I was experiencing the same problems while upgrading my code for iOS 13 scenes pattern. With parts of your second code snippet I managed to fix everything so my windows are appearing again. I was doing the same as you except for the last line. Try removing viewController.present(...). Here's my code:

let windowScene = UIApplication.shared
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first
if let windowScene = windowScene as? UIWindowScene {
popupWindow = UIWindow(windowScene: windowScene)
}

Then I present it like you do:

popupWindow?.frame = UIScreen.main.bounds
popupWindow?.backgroundColor = .clear
popupWindow?.windowLevel = UIWindow.Level.statusBar + 1
popupWindow?.rootViewController = self as? UIViewController
popupWindow?.makeKeyAndVisible()

Anyway, I personally think that the problem is in viewController.present(...), because you show a window with that controller and immediately present some 'self', so it depends on what 'self' really is.

Also worth mentioning that I store a reference to the window you're moving from inside my controller. If this is still useless for you I can only show my small repo that uses this code. Have a look inside AnyPopupController.swift and Popup.swift files.

Hope that helps, @SirOz

How to Push ViewController in new window

Swift 5.x

Below code works

took global variable:

var window2 : UIWindow?

Modified code with below

let win = UIWindow(frame: UIScreen.main.bounds)
let nc = UINavigationController()
nc.pushViewController(self, animated: true)
nc.navigationBar.isHidden = true
nc.view.backgroundColor = .clear
win.rootViewController = nc
window2 = win
win.windowLevel = UIWindow.Level.alert + 1
win.makeKeyAndVisible()

And dismiss new window I did below

if let nc = window2?.rootViewController as? UINavigationController {
nc.popViewController(animated: true)
}

window2?.resignKey()
window2 = nil

Note: It is not showing push and pop default animation, rather currently it is working w/o any kind of animation.

Ref : To create a new UIWindow over the main window

What happens to the previous window if I create a new window and makeKeyAndVisible

You can get the existing window from the AppDelegate and change the rootViewControlller

if let appDelegate = UIApplication.shared.delegate as? AppDelegate, let window = appDelegate.window {
window.rootViewController = loginVC
}

How do I add a view to the top of an app in iOS 13/14

You have to create a second UIWindow, assign the rootViewController and make it visible.

The code bellow is for SwiftUI, but you can do the same with UIKit, just create a window and set window.isHidden = false.

let secondWindow = UIWindow(windowScene: windowScene)
secondWindow.frame = CGRect(x: 0, y: 40, width: UIScreen.main.bounds.size.width, height: 100)
let someView = Text("I am on top of everything")
secondWindow.rootViewController = UIHostingController(rootView: someView)
secondWindow.isHidden = false

Depending on how many windows you have. You might need to change the windowLevel of your second window.
You can check if the window is displayed using Debug View Hierarchy from Xcode.

This is a UIKit example without SceneDelegate. If you have scene delegate you have to pass the window scene to the UIWindow init.
Don't forget to retain the second window.


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
private enum Constants {
static let sessionConfiguration = URLSessionConfiguration.default
}

var window: UIWindow?
var secondWindow: UIWindow?

// MARK: - UIApplicationDelegate

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {

let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window
let viewController = UIViewController()
viewController.view.backgroundColor = .red

window.rootViewController = viewController
window.makeKeyAndVisible()

let secondWindow = UIWindow()
secondWindow.frame = CGRect(
x: 0,
y: 40,
width: UIScreen.main.bounds.size.width,
height: 100
)

let secondController = UIViewController()
secondController.view.backgroundColor = .blue
secondWindow.rootViewController = secondController
secondWindow.isHidden = false
self.secondWindow = secondWindow

return true
}
}

Sample Image



Related Topics



Leave a reply



Submit