mredig
mredig

Reputation: 1805

Override Apple Siri remote Menu Button Default behavior

According to Apple's guidelines, pressing the menu button on tvOS should return you to the previous menu, until you're at the top menu at which point it should return you to the OS menus. My question is, how do I prevent the default behavior of the menu button and stop it from returning to the OS menus, but then reactivate it when the user is at the top menu of my app?

Upvotes: 6

Views: 3693

Answers (3)

Individual11
Individual11

Reputation: 394

Swift version of @C_X's answer, which worked for me.

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

Swift 3 version:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyClass.handleMenuPress(_:)))
tapGesture.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
self.view.addGestureRecognizer(tapGesture)

Upvotes: 7

Adnan Aftab
Adnan Aftab

Reputation: 14477

You can register a tapGestureRecognizer and set allowedPressTypes = UIPressTypeMenu code like so:

UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGestureRec.allowedPressTypes = @[@(UIPressTypeMenu)];
[self.view addGestureRecognizer:tapGestureRec];

Then whenever the Siri remotes menu button is pressed your handleTap method will be called, allowing you to add any custom logic you need. Just be aware that blocking the menu button from suspending your application on the root view controller can be a cause for App Store rejection.

You can get more information about detecting gestures here and about pressTypes here.

Upvotes: 16

lemnar
lemnar

Reputation: 4053

This behavior is something you get for free by using UINavigationController.

More generally, a container view controller can use the -gestureRecognizerShouldBegin: delegate method to allow or prevent a menu button tap gesture recognizer from beginning based on whether it's at the root level of nesting or not. When the shared UIApplication instance receives an unhandled menu button press, it will inform the system, which will cause the app to be sent to the background.

Upvotes: 4

Related Questions