How to customize shortcuts bar above the keyboard with UITextInputAssistantItem on iPad

iPadOS has a support to customize shortcuts bar displayed above the keyboard with your own buttons. You can utilize two areas in the shortcuts bar – a leading area which is positioned before the typing suggestions and a trailing area which is positioned after the typing suggestions. Each area is organized into its own groups which you need to create.

First, get the input assistant item from the responder object (e.g., text field) for which you want to customize the shortcuts bar.

let inputAssistantItem = textField.inputAssistantItem

Then, create bar button items with your custom buttons and make a group out of them.

let firstBarButtonItem = UIBarButtonItem(title: "First", style: .plain, target: self, action: #selector(itemPressed))
let secondBarButtonItem = UIBarButtonItem(title: "Second", style: .plain, target: self, action: #selector(itemPressed))
let leadingBarButtonItemGroup = UIBarButtonItemGroup(barButtonItems: [firstBarButtonItem, secondBarButtonItem], representativeItem: nil)

Finally, assign the group you just created to the leading or trailing area of shortcuts bar.

inputAssistantItem.leadingBarButtonGroups = [leadingBarButtonItemGroup]

Of course, your bar button items can be images or system items. Take a look at an example to create two system bar button items and assigning them to the trailing area of shortcuts bar.

let refreshBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(itemPressed))
let addBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(itemPressed))
let trailingBarButtonItemGroup = UIBarButtonItemGroup(barButtonItems: [refreshBarButtonItem, addBarButtonItem], representativeItem: nil)
inputAssistantItem.trailingBarButtonGroups = [trailingBarButtonItemGroup]

Take a look at the final result of our customization.

UITextInputAssistantItem with two leading text buttons and two trailing system item buttons.
UITextInputAssistantItem with two leading text buttons and two trailing system item buttons.