在tvOS中,如果我使用自定义UIWindow
实例,应用程序将停止响应模拟器中的键盘和遥控器。我应该在UIWindow
实例上设置任何变量或属性吗?
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var window : UIWindow? = {
let screen = UIScreen.main
let w = UIWindow(frame: screen.bounds)
return w
}()
// ...
}
原因是我需要将UIWindow
子类化为自定义色调,并通过traitCollectionDidChange
响应暗/亮模式更改。
这是在tvOS 10.2.1中
答案 0 :(得分:0)
您需要在UIWindow的实例中调用makeKeyAndVisible()
。
https://developer.apple.com/reference/uikit/uiwindow/1621601-makekeyandvisible
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func applicationDidFinishLaunching(_ application: UIApplication) {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = yourRootViewController
window?.makeKeyAndVisible()
}
}
答案 1 :(得分:-1)
如果需要自定义UIWindow
,显然需要实例化故事板并显示窗口。仅提供UIWindow
实例是不够的。
首先,从主应用的UIMainStoryboardFile
文件中删除密钥Info.plist
。
然后在应用程序中添加代码确实启动处理程序:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
// ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = MainWindow(frame: UIScreen.main.bounds)
// We need to instantiate our own storyboard instead of specifying one in `Info.plist` since we need our own custom `UIWindow` instance.
// Otherwise if we just create the custom UIWindow instance and let the system creates a storyboard,
// then the application won't respond to the keyboard/remote (user input).
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
window?.rootViewController = storyboard.instantiateInitialViewController()
defer {
window?.makeKeyAndVisible()
}
// ... All other setup code
}
// ...
}