3

After reading http://nshipster.com/uikeycommand/ i wanted to add navigation between tabs using arrow keys to my app. But i cuold not managed to receive key presses within UITabBarController. Same code block below works within UIViewController but UITabBarController.

When i tried to debug, canBecomeFirstResponder override for UITabBarController not even get called. I did not find something useful on apple s docs. Appreciate any information which makes the problem clear.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let firstResponderResult = self.becomeFirstResponder()
    println("became first responder \(firstResponderResult)")
}

override func canBecomeFirstResponder() -> Bool {
    return true
}

override var keyCommands: [AnyObject]? {
    return [
        UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: UIKeyModifierFlags.allZeros, action: "rightArrowClicked:"),
        UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: UIKeyModifierFlags.allZeros, action: "leftArrowClicked:")
    ]
}

func rightArrowClicked(sender: UIKeyCommand) {

    let currentIndex = selectedIndex;

    if (currentIndex < viewControllers!.count - 1) {
        selectedIndex = currentIndex + 1;
    }
}

func leftArrowClicked(sender: UIKeyCommand) {
    let currentIndex = selectedIndex;

    if (currentIndex > 0) {
        selectedIndex = currentIndex - 1;
    }
}
mehmet6parmak
  • 4,777
  • 16
  • 50
  • 71
  • Did this work for you? In my case, adding the `canBecomeFirstResponder` works initially, but after the keyboard is used to move to a different UIViewController (which also has the same `canBecomeFirstResponder` and keyCommands), the 2nd view controller doesn't respond to keyboard input. It seems like it doesn't transfer the firstResponder status to the 2nd VC. Is that true in your case as well? – Z S Nov 11 '15 at 07:13
  • Not worked unfortunately, i had the same situation as you. I think, the only work around which will work is the answer below but i did not tried that too just gave up that implementing this feature. – mehmet6parmak Nov 11 '15 at 07:53

2 Answers2

2

I think you should override canBecomeFirstResponder method of VC's inside the UITabBarController, not the tab bar controller's. This way you should be able to get key presses.

CanC
  • 3,295
  • 1
  • 14
  • 15
0

iOS13 and Mac Catalyst in Swift5:

extension UITabBarController {

    open override var keyCommands: [UIKeyCommand]? {
        return [
            UIKeyCommand(input: UIKeyCommand.inputRightArrow, modifierFlags: .alternate, action: #selector(rightArrowClicked)),
            UIKeyCommand(input: UIKeyCommand.inputLeftArrow, modifierFlags: .alternate, action: #selector(leftArrowClicked))
        ]
    }

    @objc private func rightArrowClicked(sender: UIKeyCommand) {

        let currentIndex = selectedIndex;

        if (currentIndex < viewControllers!.count - 1) {
            selectedIndex = currentIndex + 1;
        }
    }

    @objc private func leftArrowClicked(sender: UIKeyCommand) {
        let currentIndex = selectedIndex;

        if (currentIndex > 0) {
            selectedIndex = currentIndex - 1;
        }
    }

    open override var canBecomeFirstResponder: Bool {
        return true
    }
iHTCboy
  • 2,715
  • 21
  • 20