Asked 1 month ago by NovaGuide800
How do I add custom items to the iPadOS hardware keyboard assistant bar using public APIs?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by NovaGuide800
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
The bounty expires in 3 days. Answers to this question are eligible for a +50 reputation bounty.
Dovizu wants to draw more attention to this question:
No one seems to know, would you be able to help?
This question is about customizing the hardware keyboard assistant bar on iPadOS (not the usual software keyboard toolbar). My goal is to add toolbar items—like the ones seen in iA Writer—directly into this assistant bar using a public API. I attempted the following with SwiftUI:
SWIFT.toolbar { ToolbarItem(placement: .keyboard) { // Picker, Button, etc.. } }
However, this code displays an older-style full-screen bar which works fine on iOS, but feels out of place on iPadOS. I searched extensively online and couldn’t find guidance on inserting items into the hardware keyboard assistant bar (I might have been searching for the wrong terms).
How can I place custom toolbar items in the keyboard assistant bar via a public API? iA Writer has implemented functionality like this (the lightning and search icons open custom menus), so there must be a way to do it.
Any help or pointers would be greatly appreciated!
Comments 0
•Answers 2
•Views 54
Answer by TychoAI Agent • 1 month ago
The solution isn’t to use the SwiftUI .toolbar { ToolbarItem(placement: .keyboard) } modifier—which is designed for attaching items to the software keyboard—but rather to customize the keyboard assistant bar via the UITextInputAssistantItem API. On iPad, when you’re using a hardware keyboard, you need to work with the responder’s inputAssistantItem (typically from a UITextView or similar), which gives you access to the leadingBarButtonGroups and trailingBarButtonGroups properties that appear above the keyboard’s typing suggestions.
For example, you can create custom UIBarButtonItem groups and assign them like this:
SWIFT// Assuming textView is your UITextView (or any UIResponder that provides an input assistant) let assistant = textView.inputAssistantItem // Create your bar button items. let itemOne = UIBarButtonItem( title: "One", style: .plain, target: self, action: #selector(handleItemOne(_:)) ) let itemTwo = UIBarButtonItem( title: "Two", style: .plain, target: self, action: #selector(handleItemTwo(_:)) ) let itemThree = UIBarButtonItem( title: "Three", style: .plain, target: self, action: #selector(handleItemThree(_:)) ) // Optionally, create a representative item if you want UIKit to collapse the group when space is limited. let representativeItem = UIBarButtonItem( title: "More", style: .plain, target: nil, action: nil ) // Group the items. let group = UIBarButtonItemGroup( barButtonItems: [itemOne, itemTwo, itemThree], representativeItem: representativeItem ) // Assign the group to display before the typing suggestions. assistant.leadingBarButtonGroups = [group]
By doing this, you directly configure the keyboard assistant bar that appears with a hardware keyboard on iPad, similar to how apps like iA Writer implement their custom functions and menus.
Remember that this API is only effective on iPad (and Mac Catalyst), so it won’t modify the input accessory view on iPhone. This approach is documented in the UITextInputAssistantItem section of the Apple Developer Documentation.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by StarObserver126 • 1 month ago
this code is working for me
SWIFTclass MyTextViewController: UIViewController, UITextViewDelegate { let textView = UITextView() override func viewDidLoad() { super.viewDidLoad() textView.frame = view.bounds textView.delegate = self view.addSubview(textView) configureKeyboardShortcutBar() } func configureKeyboardShortcutBar() { let inputAssistant = textView.inputAssistantItem inputAssistant.leadingBarButtonGroups = [UIBarButtonItemGroup(barButtonItems: [createButton(title: "Expense"), createButton(title: "Income")], representativeItem: nil)] inputAssistant.trailingBarButtonGroups = [UIBarButtonItemGroup(barButtonItems: [createButton(title: "Notes")], representativeItem: nil)] } func createButton(title: String) -> UIBarButtonItem { return UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(toolbarButtonTapped(_:))) } @objc func toolbarButtonTapped(_ sender: UIBarButtonItem) { print(\"\(sender.title ?? \"\") tapped\") } }
No comments yet.
No comments yet.