Shortcut bar API

Hello,


I'm currently working on a text editor for IOS and want to add custom items to the new shortcut bar but I can't find a way to add them. What I've found out so far is that there's the new UITextInputAssistantItem class but I can't find a way to add to add the items to the keyboard.


Also is there a way to disable the shortcut bar?

Accepted Reply

The UITextInputAssistantItem contains 2 arrays, leadingBarButtonGroups and trailingBarButtonGroups. You can add instances of another new class, UIBarButtonItemGroup, to these arrays to add or replace items on the short cuts bar. UIBarButtonItemGroup has 2 major properties, barButtonItems and representativeItem, that you can in turn fill out to specify what is on the bar. Finally to hide the bar (assuming that predictive text is also off) you can set the leading & trailing groups to an empty array.


Example:

NSArray barButtonItems = @[ /* Create UIBarButtonItems and place them here */ ];
UIBarButtonItem representativeItem = // This is optional, if present when there isn't enough room on the bar, the group will be replaced with this item
UIBarButtonItemGroup *group = [[UIBarButtonItemGroup alloc] initWithBarButtonItems:barButtonItems representativeItem:representativeItem]
textView.inputAssistantItem.leadingBarButtonGroups = @[ group ]; // replace the items on the bar. Alternatively you can append this group and get the default leading items with this extra group


Hopefully that gets you started.

Replies

The UITextInputAssistantItem contains 2 arrays, leadingBarButtonGroups and trailingBarButtonGroups. You can add instances of another new class, UIBarButtonItemGroup, to these arrays to add or replace items on the short cuts bar. UIBarButtonItemGroup has 2 major properties, barButtonItems and representativeItem, that you can in turn fill out to specify what is on the bar. Finally to hide the bar (assuming that predictive text is also off) you can set the leading & trailing groups to an empty array.


Example:

NSArray barButtonItems = @[ /* Create UIBarButtonItems and place them here */ ];
UIBarButtonItem representativeItem = // This is optional, if present when there isn't enough room on the bar, the group will be replaced with this item
UIBarButtonItemGroup *group = [[UIBarButtonItemGroup alloc] initWithBarButtonItems:barButtonItems representativeItem:representativeItem]
textView.inputAssistantItem.leadingBarButtonGroups = @[ group ]; // replace the items on the bar. Alternatively you can append this group and get the default leading items with this extra group


Hopefully that gets you started.

I am trying to hide the extra bar, I can remove the elements but the bar takes up space on the screen. Any ideas.


Here is my code:


NSArray *barButtonItems = @[ ];

UIBarButtonItemGroup *group = [[UIBarButtonItemGroup alloc] initWithBarButtonItems:barButtonItems representativeItem:nil];

self.inputAssistantItem.leadingBarButtonGroups = @[ group ];

self.inputAssistantItem.trailingBarButtonGroups = @[ group ];

To hide the extra bar, this works for me:


  textview.inputAssistantItem.trailingBarButtonGroups = @[];
  textview.inputAssistantItem.leadingBarButtonGroups = @[];

Vladimir's solution is the supported way to hide the bar, although we generally would only recommend doing so if you have some other bar (such as an input view) an have no need for the short cuts bar in tandem with it.

I have an own input view, but since iOS 9 beta 3 an empty extra bar appears on iPads. Is this a bug?

You should never see an empty bar (we've had bugs on this that we've been steadily fixing, but an empty bar means a bug).


I presume you mean you just have an input view with an standard Shortcuts bar on the input responder – if you are seeing a case where your custom items aren't showing up on the bar, post what you are doing.

I have an input view and i have set empty bar button groups in the input assistant item. Until iOS 9 beta 2 it works fine. Ther was no extra bar above my input view. Since iOS 9 beta 3 an empty bar is shown above my input view.

Sounds like its just a bug, could you file it, attach a simple example, and report the bug number here?

Problem is filed with the number 21978519.

Hi,

Since Beta 5, the shortcut bar appears on top of third party keyboards as well which is nice. There are two problems to this approach though. I wish someone can help me on this.

1. The keyboards would take much more space on the screen.

2. I couldn't find any way to customize the contents and looks of appeared shortcut bar. That would be very cool to be able to change the background color of the newly added bar to match the keyboard. Also, being able to fill the huge empty space with for example the prediction bar of the keyboard is a nice addition.

I should mention that I'm a third party keyboard developer and want to have mentioned features for my development purposes.

3rd party keyboards can't affect the content of the Shortcuts bar – the content is primarily reserved for the frontmost app.


That said, being able to present some kind of keyboard specific content similar to predictions is not an unreasonable request, neither is the ability to theme it to better match your 3rd party keyboard. Feel free to Report Bugs asking for these (seperate requests please).

We have a custom text view which implements UITextInput protocol. What methods do we need to implement in our text view if we want to hide the shortcut bar? UITextInput protocol doesn't seem to have any relevant ones.

You don't need to implement any methods, -inputAssistantItem is a method on UIResponder, so just call that method and do what I mentioned earlier.

Problem is solved in iOS 9 beta 5.

I just filed a report to allow developers to hide the shortcuts bar for their custom keyboard, just like Apple hides it for their Emoji keyboard - 22493048. Customzing its appearance (and contents) would also be great, otherwise developers will just hide it if they can't make it look good with their keyboard.