2017-05-14 2 views
0

Dans tvOS, si j'utilise une instance personnalisée UIWindow, l'application cesse de répondre au clavier et à la télécommande dans le simulateur. Y at-il une variable ou une propriété que je devrais définir sur l'instance UIWindow?UIWindow personnalisé dans tvOS empêche l'application de répondre à l'entrée au clavier

class AppDelegate: UIResponder, UIApplicationDelegate { 

    lazy var window : UIWindow? = { 
     let screen = UIScreen.main 
     let w = UIWindow(frame: screen.bounds) 
     return w 
    }() 

    // ... 
} 

La raison en est que je dois sous-classe UIWindow pour avoir des couleurs de teinte personnalisées et de répondre aux changements de mode noir/lumière via traitCollectionDidChange.

C'était dans tvOS 10.2.1

Répondre

-1

Apparemment, on aurait besoin d'instancier le story-board et présenter la fenêtre et si nécessaire est une coutume UIWindow. Fournir simplement une instance UIWindow ne serait pas suffisant.

D'abord, supprimer la UIMainStoryboardFile clé du fichier Info.plist de votre application principale.

Puis ajouter dans le code dans l'application ne lancer gestionnaire à:

  1. Instantiate la fenêtre et attribuer à la propriété du délégué de l'application.
  2. Instancier le storyboard. Installez le contrôleur de vue initial
  3. Affectez le contrôleur de vue à la fenêtre.
  4. Afficher la fenêtre.
@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 
     } 

     // ... 

    } 
0

Vous devez appeler makeKeyAndVisible() dans votre instance de UIWindow.

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() 
    } 
}