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
"Message from Debugger: Unable to Attach" When Running Tests on Osx App
Sharing File Data Between Applications in Swift/Ios
Nscollectionviewitem Never Instantiate
Tapping an Mkmapview in Swiftui
Value of Type 'Tags' Has No Member 'Lastused'
Swift Callkit Sometimes Can't Activate Loudspeaker After Received Call (Only Incoming Call)
Given a Function Parameter of Type [Int]; Can It Be Constrained to Not Be Empty
How to Convert a Unsafemutablepointer<Void> to Uint8
How to Connect Outlets and Actions in Swift/Macos Programaticly
Swift - Kvo - Change.Newvalue and Change.Oldvalue Are Nil
Swift. Is the (Absolutely) Sole Specific Advantage of Unowned Over Weak, Performance
Is Is Possible to Use Vapor 3 Postgres Fluent in a Standalone Script
If-Let Any to Rawrepresentable<String>
Deep Copy of Cmimagebuffer or Cvimagebuffer
Firestore Query Across Multiple Fields for Same Value
Text from [String] to a Label.Text Isn't Working the First Time