2017-05-14 53 views
0

在tvOS中,如果我使用自定義UIWindow實例,則應用程序將停止響應模擬器中的鍵盤和遠程設備。我應該在UIWindow實例上設置變量或屬性嗎?tvOS中的自定義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

回答

-1

顯然,一個需要實例化的故事板,如果需要定製UIWindow呈現窗口爲好。僅僅提供一個UIWindow實例是不夠的。

首先,刪除密鑰UIMainStoryboardFile從您的主應用程序的Info.plist文件。

然後在代碼中應用程序添加到都推出處理程序:

  1. 實例化窗口和分配給應用程序委託的財產。
  2. 實例化故事板。
  3. 實例化初始視圖控制器
  4. 將視圖控制器分配給窗口。
  5. 顯示窗口。
@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

你需要調用makeKeyAndVisible()在一個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() 
    } 
} 
相關問題