0

I'm trying to create an overlay I can use from any view controller to host preview images for draggable views.

From my AppDelegate.swift

import UIKit
import MaterialComponents

var myOverlayWindow: MDCOverlayWindow?

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window!.overrideUserInterfaceStyle = .dark
        // Override point for customization after application launch.
        myOverlayWindow = MDCOverlayWindow.init(windowScene: window!.windowScene!)
        return true
    }


}

and in another View Controller:

func showOverlay() {
    let testView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
    testView.backgroundColor = .systemRed
    myOverlayWindow!.activateOverlay(testView, withLevel: .normal)
    print("overlay should show")
}

Why isn't the overlay activating and displaying testView? Am I instantiating MDCOverlayWindow incorrectly?

zakdances
  • 22,285
  • 32
  • 102
  • 173

1 Answers1

0

The MDCOverlayWindow has to be the actual window attached to the screen by UIKit. You created an instance and saved it in a property. But you didn't tell UIKit about that window. Presumably, you are using a storyboard. The storyboard instantiation process handles attaching the UIWindow with UIKit and all the scenes are rendered in that window. I'm honestly not sure whether you can specify a custom window class with a storyboard. I opened a storyboard-based project and poked around, but didn't see any window customization options in the inspectors.

If you are not using a storyboard, then you can create the window and attach it to the screen, it looks like this: (excerpt of application:didFinishLaunching:)

  window = MDCOverlayWindow(frame:UIScreen.main.bounds)
  window!.rootViewController = LaunchViewController()
  window!.backgroundColor = UIColor.white
  window!.makeKeyAndVisible() // this is the key: window is actually rendered
androidguy
  • 3,005
  • 2
  • 27
  • 38