Search code examples
tvos

How to customize back navigation on Apple tvOS?


I am developing an application for tvOS. In my app users select their country, states and districts respectively in different view controllers. After selections, App displays a page with the information related to users' selections. When user presses the menu button in this page, I want the app to exit, but the app goes to previous page which is district selection view controller.

How can I make the app exit in this situation. I don't want user to go back all the pages to exit the app.


Solution

  • I found the solution from this question that Daniel Storm mentioned. Here is the swift code:

    override func viewDidLoad(){
        let tapRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
        tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
        self.view.addGestureRecognizer(tapRecognizer)
    }
    
    func handleTap(gesture: UITapGestureRecognizer){
        if gesture.state == UIGestureRecognizerState.Ended {
            let app = UIApplication.sharedApplication()
            app.performSelector(Selector("suspend"))
        }
    }
    

    I hope it helps someone.