I'm using a standard UITabBarController on iPad. When first selecting any tab, the corresponding menu bar items are grayed out for this view controller. It's only when I tap any button in that view controller, like in the toolbar, that the view controller truly becomes the first responder (you can see the sidebar selection turns to gray from blue), enabling those menu bar items.
Am I doing something wrong here?
A video of the issue can be found here: https://mastodon.social/@nicoreese/114949924393554961
AppDelegate:
...
builder.insertChild(MenuController.viewModeMenu(), atStartOfMenu: .view)
class func viewModeMenu() -> UIMenu {
let listViewModeCommand = UICommand(
title: String(localized: "As List"),
image: UIImage(systemName: "list.bullet"),
action: #selector(GamesViewController.setListViewMode),
propertyList: SettingsService.ViewMode.list.rawValue
)
...
let viewModeMenu = UIMenu(
title: "",
image: nil,
identifier: .viewModeMenu,
options: .displayInline,
children: [listViewModeCommand...]
)
return viewModeMenu
}
GamesViewController:
@objc
func setListViewMode() {
updateViewMode(.list)
}
I can do this, but then the sidebar selection instantly turns gray, which looks odd and other system apps do not behave this way.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
becomeFirstResponder()
}
override var canBecomeFirstResponder: Bool {
return true
}
The solution here is largely going to depend on what behavior you want for your app. When you tap an item in the tab sidebar, for example, focus moves to the sidebar so you can hit the arrow keys to move up and down tabs. On the other hand, if your app wants arrow keys to navigate the actual view controller opened by that tab, you likely want a solution like you have where you make your view controller first responder in viewDidAppear.
The sidebar changing colors when the detail view controller becomes first responder is expected behavior—it is the way the system indicates to the user that focus is moving out of the sidebar.
If you want your keyboard shortcuts to work on both the sidebar and the detail view controller, you may want to implement the actions on a higher-level responder like the UITabBarController/UISplitViewController itself, and then have the implementation call into the appropriate child view controller.