Keyboard leave a blank space after saving

Viewed 221

Since the new Swift version, it looks like the keyboard leaves a blank space when I save the data.

MyView

import SwiftUI

struct AddUpdateUrl: View {
    
    @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
    @EnvironmentObject var controller: PlacesViewController
    
    @Binding var place: Place
    @State var url = ""
    
    var updateMode = false
    
    var body: some View {
        VStack {
            Text("Website")
                .font(.callout)
            TextField("", text: $url)
                .keyboardType(.URL)
                .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: 1)
        }
        .navigationBarItems(
            trailing: Button(action: {save()}) {Text("Save")}.disabled(url.isEmpty)
        )
        .resignKeyboardOnDragGesture()
    }
    
    private func save() {
        if !url.isEmpty {
            place.url = url
            if updateMode {
                controller.update(place: place)
            }
        }
        presentationMode.wrappedValue.dismiss()
    }
    
}

And the result. The view before trying to update the website The view before trying to update the Webiste

The view to add the website The view to add the webiste

After clicking the save button After clicking the save button

I have the same behavior on the simulator and on my iPhone 11 (ios 14.3)

The .resignKeyboardOnDragGesture() is an extension that removes the keyboard.

    struct ResignKeyboardOnDragGesture: ViewModifier {
    var gesture = DragGesture().onChanged {_ in
        UIApplication.shared.endEditing(true)
    }
    func body(content: Content) -> some View {
        content.gesture(gesture)
    }
}

extension View {
    func resignKeyboardOnDragGesture() -> some View {
        return modifier(ResignKeyboardOnDragGesture())
    }
}
0 Answers
Related