我正在尝试创建一个叠加层,我可以在任何视图控制器中使用该叠加层来托管可拖动视图的预览图像。
从我的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
}
}
以及另一个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")
}
为什么重叠式广告无法激活并显示testView
?我是否错误地实例化了MDCOverlayWindow?