Siri Remote's Menu Button not exiting application when UIButton is focused
Asked Answered
A

1

2

I'm overriding pressesBegan to receive Select presses. The Siri Remote's Menu Button does not exit my application when the focus is on a UIButton. If no UI element is focused the Menu Button works as expected.

How do I receive Menu Button presses when the focus is on a UIButton?

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        if item.type == .Select {
            print("Click")
        }
        if item.type == .Menu {
            print("Menu Button")
            super.pressesBegan(presses, withEvent: event)
        }
    }
}
Attwood answered 7/6, 2016 at 17:26 Comment(0)
A
2

UIButton only responds to the .Select button on the remote. You can catch the menu button using a tap gesture recognizer. For example:

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ClassName.menuPressed()))
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Menu.rawValue)];
view.addGestureRecognizer(tapRecognizer)

Then implement:

func menuPressed() {
    // do something
}
Alvira answered 7/6, 2016 at 17:39 Comment(2)
This works great. Forgot about using a UITapGestureRecognizer. Is there a way to return to the home screen gracefully rather than exit(0)?Attwood
Nevermind, got it: UIControl().sendAction(#selector(NSURLSessionTask.suspend), to: UIApplication.sharedApplication(), forEvent: nil) Thank you!Attwood

© 2022 - 2024 — McMap. All rights reserved.