How to Handle Menu Button Action in Tvos Remote

How to handle Menu Button action in tvOS remote

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

tvOS - catch MENU button presses during push-view-controller transition

A solution that worked for me was to install a UITapGestureRecognizer onto self.navigationController.view. Any taps which get missed by the regular UIViewControllers end up getting caught by the navigation controller's recognizer instead. If you install UITapGestureRecognizers on each view controller you create, the only taps which will fall through the cracks to the navigation controller are taps that occur mid-transition, and it is safe to just ignore those taps completely.

Note that when you want MENU to go back to the home screen, you will need to temporarily remove this tap recognizer and let tvOS handle the tap on its own, as I could find no clean way to escape to the home screen in my code (short of exit(0)).

tvOS Menu Button default behavios

You can find the default behavior of Menu button in the apple guidelines here : https://developer.apple.com/tvos/human-interface-guidelines/remote-and-controllers/

To make it clear:
Say you are in 2nd view controller which you reached after being pushed from 1st view controller. Now on tapping the Menu button in 2nd VC you will be popped back to the 1st VC.
If you tap the Menu button again in 1st VC, the app will be put to background and you will be taken to Apple TV home screen.

Coming to accessing the Menu button part:
I wasn't able to override the default behavior but I think you can listen for it by overriding the pressesBegan method.

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

switch presses.first!.type {

case UIPressType.Menu : // Your code here
default : break

}

Apple TV – How to only change focus when Menu button is tapped

Eventually, I noticed overriding pressesEnded won't cancel the Menu button press for the first time after launching the app. Using UITapGestureRecognizer on the other hand does:

    let menuTapGestureRegocnizer = UITapGestureRecognizer(target: self, action: "menuTapped")
menuTapGestureRegocnizer.allowedPressTypes = [UIPressType.Menu.rawValue]
view.addGestureRecognizer(menuTapGestureRegocnizer)

UIAlertController dismiss on tvOS with menu button

@ion: Your answer led me to the right answer.

You indeed need to add one action with style UIAlertActionStyleCancel to have the menu button to work as expected and exit the UIAlertViewController.
To have this Cancel action hidden from the options (no button, like in the Settings app), just set its Title and Handler to nil:

[alert addAction:[UIAlertAction actionWithTitle:nil style:UIAlertActionStyleCancel handler:nil]];


Related Topics



Leave a reply



Submit