简体   繁体   中英

How to prevent keyboard dismiss when bottomsheet is dismiss using swipe gesture (Interactive dismiss) iOS

I have a requirement where we need to keep close active unless bottom sheet is not closed. I have seen similar implementations in iOS maps where search keyboard is opened until the view is fully dismissed.

What I have currently:- 在此处输入图像描述

What I want:- https://drive.google.com/file/d/1SmXniFp0ZTF5igMzk6gk3eflCeP1xjn6/view?usp=share_link

This is code which i use to to present iOS native sheet:-

struct ContentView: View {
    @State var isPresented = false

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .onTapGesture {
            isPresented.toggle()
        }
        .sheet(isPresented: $isPresented) {
            BottomSheetViewRepresentable(content: {
                NavigationView {
                    DemoView()
                }
                .navigationTitle("Hey")
            }, detents: [.large()])
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct DemoView: View {
    var body: some View{
        if #available(iOS 16.0, *) {
            ZStack {
                TextField("", text: .constant("Hey"))
            }
            .scrollDismissesKeyboard(.never)
        } else {
            // Fallback on earlier versions
            Color.yellow
        }
    }
}

I see you have added .scrollDismissesKeyboard(.never) on the ZStack which seems not working. Try wrapping content in a List or ScrollView , then set scrollDismissesKeyboard to never. Examples -

var body: some View {
        ScrollView {
            VStack {
                TextField("Name", text: $username)
                    .textFieldStyle(.roundedBorder)
                TextEditor(text: $bio)
                    .frame(height: 400)
                    .border(.quaternary, width: 1)
            }
            .padding(.horizontal)
        }
        .scrollDismissesKeyboard(.never)
    }

List example -

struct KeyboardDismissExample: View {
        @State private var text = ""
        
        var body: some View {
            List {
                ForEach(0..<100) { i in
                    TextField(("Item \(i)"), text: .constant(""))
                }
            }
            .scrollDismissesKeyboard(.never)
    
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM