How to Make Pure SwiftUI Keyboard Toolbar?

How to Make Pure SwiftUI Keyboard Toolbar?

In this tutorial we are going to implement a keyboard wrapper view. This view at root level can show/hide custom keyboard toolbar and manage view hight.

Xcode 12 — iOS 14

Image for post

In this tutorial we are going to implement a keyboard wrapper view. This view at root level can show/hide custom keyboard toolbar and manage view hight.

Steps:

  • Create a generic view which takes content and toolbar view callbacks as parameter.
struct KeyboardView<Content, ToolBar> : View where Content : View, ToolBar: View {

 @StateObject private var keyboard: KeyboardResponder = KeyboardResponder()
 let toolbarFrame: CGSize = CGSize(width: UIScreen.main.bounds.width, height: 40.0)
 var content: () -> Content
 var toolBar: () -> ToolBar
 var body: some View {
  //to do 
 }

}
  • Create a Keyboard observer(picked from here):
    struct KeyboardView<Content, ToolBar> : View where Content : View, ToolBar: View {
        @StateObject private var keyboard: KeyboardResponder = KeyboardResponder()
        let toolbarFrame: CGSize = CGSize(width: UIScreen.main.bounds.width, height: 40.0)
        var content: () -> Content
        var toolBar: () -> ToolBar

        var body: some View {
            ZStack {
                content()
                    .padding(.bottom, (keyboard.currentHeight == 0) ? 0 : toolbarFrame.height)
                VStack {
                     Spacer()
                     toolBar()
                        .frame(width: toolbarFrame.width, height: toolbarFrame.height)
                        .background(Color.secondary)
                }.opacity((keyboard.currentHeight == 0) ? 0 : 1)
                .animation(.easeOut)
            }
            .padding(.bottom, keyboard.currentHeight)
            .edgesIgnoringSafeArea(.bottom)
            .animation(.easeOut)

        }
    }

programming swiftui swift keyboard toolbar

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

10 Free Online Resources To Learn Swift Language

Swift is a fast and efficient general-purpose programming language that provides real-time feedback and can be seamlessly incorporated.

ToolbarSUI: A SwiftUI Toolbar

An open source UIKit-like toolbar. Before SwiftUI 2, there was no way of creating a toolbar like in UIKit, so I decided to implement my own. It is available on GitHub as a Swift package called ToolbarSUI.

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup? **[Hire Dedicated Swift Developers](https://hourlydeveloper.io/hire-dedicated-swift-developer/ "Hire Dedicated Swift Developers")** for end-to-end services like development, migration...

ios - Move view with keyboard using Swift - Stack Overflow

ios - Move view with keyboard using Swift - Stack Overflow ios - Move textfield when keyboard appears swift -When the user taps in a text field, that text fi...

How To Stack Views & Use Modifiers Efficiently in SwiftUI

There are three built-in stacking views in SwiftUI; VStack, HStack, and ZStack. All three stacks are of type View. Stacks have alignment and content parameters.