Allow Menu button to exit tvOS app when pressed on presented modal view controller

戏子无情 提交于 2019-12-01 12:14:36

问题


I have a tvOS app, with a UITabBarController as the main entry point for the main storyboard. If the user is not logged in, the UITabBarController presents a LoginViewController modally.

When the user presses the Menu button on the Siri remote, the system automatically dismiss the LoginViewController and shows the UITabBarController. I want the app to instead exit to the tvOS headboard.

Expect the solution to involve either a UITapGestureRecognizer to override the Menu button action, or overriding pressesBegan/pressesTouched, but I haven't found a solution yet.


回答1:


This following snippet works, and is acceptable behavior based on the expected behavior for a menu button in Apples HIG for Remotes and Interactions

override func viewDidLoad() {
    super.viewDidLoad()

    let tapRecognizer = UITapGestureRecognizer(target: self, action: "tapped")
    tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)];
    self.view.addGestureRecognizer(tapRecognizer)
}

func tapped() {
    exit(EXIT_SUCCESS)
}



回答2:


I had a very similar situation, it looked like this:

               +--------------------+
        ------>| MainViewController |
               +--------------------+
                         |
           +-------------+-------------+
           |                           |
           v                           v
+---------------------+      +--------------------+
| LoginViewController |      | HomeViewController |
+---------------------+      +--------------------+

If either LoginViewController or HomeViewController are presented then I want the menu button to dismiss the app and return to the TV home screen, not to MainViewController.

After much Googling, I settled upon this solution and it seems to work well without triggering warnings or requiring a call to exit.

class MainViewController: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
        UIApplication.shared.keyWindow?.rootViewController = segue.destination
    }
}

Hope this is helpful to somebody out there 🙂



来源:https://stackoverflow.com/questions/34522004/allow-menu-button-to-exit-tvos-app-when-pressed-on-presented-modal-view-controll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!