Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Menu Button action in tvOS remote

I am facing the strange problem with my application. Actually when i am presenting a view controller for play the video. At the video load time user press the menu button the application goes to background. While i have overwrite the Menu Button Action.

This is my code.

override func viewWillAppear(animated: Bool) {
    let menuPressRecognizer = UITapGestureRecognizer()
    menuPressRecognizer.addTarget(self, action: #selector(VideoPlayerViewController.menuButtonAction(_:)))
    menuPressRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.hashValue)]
    self.playerController.view.addGestureRecognizer(menuPressRecognizer)
}

func menuButtonAction(ges:UITapGestureRecognizer) {
    self.dismissView()
 }
like image 869
Sankalap Yaduraj Singh Avatar asked Nov 16 '16 14:11

Sankalap Yaduraj Singh


People also ask

What is the Menu button on Apple TV remote?

The Menu button has been renamed Back (<) button in the latest model Siri Remote, but it functions the same. The triple-click has to be somewhat speedy, to be seen as a single signal. The "Menu" button was replaced with the "back" < button.

Why is the Menu button not working on my Apple TV remote?

Restart your remote If your remote still isn't working as expected, try restarting your remote. Press and hold the TV/Control Center button and the Volume Down button at the same time. Hold the buttons down for about 5 seconds, or until the status light on your Apple TV turns off and on again. Release the buttons.

What is Menu button on new Apple TV?

MENU - This button is used to activate hidden options of a page. It also allows you to return to the previous screen. Additionally, you can use this button to turn on your Apple TV when it is off. A long press on the Menu button allows you to return to the Apple TV home screen.


1 Answers

This is my code and working for me.

Swift 3

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let menuPressRecognizer = UITapGestureRecognizer()
    menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
    menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
    self.view.addGestureRecognizer(menuPressRecognizer)
}

func menuButtonAction(recognizer:UITapGestureRecognizer) {
    self.dismiss(animated: true, completion: nil)
}

Swift 4

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let menuPressRecognizer = UITapGestureRecognizer()
    menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
    menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
    self.view.addGestureRecognizer(menuPressRecognizer)
}

@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
    self.dismiss(animated: true, completion: nil)
}

Swift 4.2 & 5

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let menuPressRecognizer = UITapGestureRecognizer()
    menuPressRecognizer.addTarget(self, action: #selector(YourViewController.menuButtonAction(recognizer:)))
    menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]
    self.view.addGestureRecognizer(menuPressRecognizer)
}

@objc func menuButtonAction(recognizer:UITapGestureRecognizer) {
    self.dismiss(animated: true, completion: nil)
}
like image 170
anas.p Avatar answered Sep 21 '22 04:09

anas.p