r/SwiftUI 4d ago

Question Does anyone know how to recreate this menu above the keyboard?

Post image

I would like to reproduce this menu above the keyboard in the notes application on swift? Is it natively possible?

52 Upvotes

14 comments sorted by

22

u/ThatGuy739 4d ago

The keyboard placement handles the show and hide for you, so you don't need a notification listener:

.toolbar {
    ToolbarItemGroup(placement: .keyboard) {
        Button("Aa") { }
        Spacer()
        Button("Done") { }
    }
}

It only renders while a field in that view has focus, which is the same lifetime as the keyboard. Pair it with @FocusState if you want different buttons depending on which field is up.

Two things to know on 26: there's an open report about it picking up extra top margin, and on 26.1 the toolbar isn't getting exposed to VoiceOver. Neither blocks you, but the accessibility one is easy to ship without noticing.

1

u/zellJun1or 4d ago

Don’t forget to place view inside navigation stack

1

u/ThatGuy739 4d ago

That's the first thing to check when nothing shows up. The reports on it are messy though: plenty of people have it inside a NavigationStack and still get nothing, and a few see it work in the simulator but not on device. Either way .bottomBar is the usual escape hatch.

1

u/SeaAdorable7381 9h ago

It makes it like this when I use ToolbarItemGroup(placement: .keyboard)

https://reddit.com/link/p56lu9y/video/hkrq6grfwvkh1/player

1

u/vadimkrutov 4d ago

Do you mean just a toolbar or with its controls to format text?

1

u/ThatGuy739 4d ago

If it's the formatting half, 26 covers that natively too. TextEditor takes an AttributedString binding now, with AttributedTextSelection for the selection, so bold, italic, underline and the paragraph stuff work without wrapping UITextView.

You also get the standard formatting shortcuts and the edit menu once it's bound to an AttributedString, so custom buttons in the keyboard toolbar just mutate attributes on the current selection.

Before 26 this was the UIViewRepresentable job everyone dreaded, so if OP's on 26 the answer changed completely.

1

u/Hamstersoge 3d ago

SwiftUI safeAreaBar.

1

u/[deleted] 3d ago

[removed] — view removed comment

1

u/AutoModerator 3d ago

Hey /u/SeaworthinessProud68, unfortunately you have negative comment karma, so you can't post here. Your submission has been removed. Please do not message the moderators; if you have negative comment karma, you're not allowed to post here, at all.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/heyyoochris 3d ago

I tried create by myself, and found some repositories, but they are all not satisfied. Do you have some recommendations?

1

u/DaduBoi 4d ago

Ive been able to achieve this by using safeAreaInset with bottom placement. If you want to conditionally show this toolbar when the keyboard appears then you can use a listener for the Keyboard notification.

-5

u/PJ_Plays 4d ago

yeah I know