当键盘与通知一起出现时,视图与键盘有很大的分隔空间

问题描述

我有这个代码。一旦键盘出现,包括 textedit 在内的一些视图就会消失,并且在键盘上方创建一个全新的空间。我想要的只是向上移动视图,以便我看到整个 textEdit 视图和所有视图,而没有在附加图像中标记为红色的整个空间。这是因为我添加键盘通知,因为我想在没有键盘隐藏的情况下查看文本编辑的完整视图。如何在没有键盘隐藏的情况下显示视图。我当前使用 keyBoard 通知解决方案在键盘上方创建了一个奇怪的间距。我在使用修饰符时附加了图像

 struct ContentView: View {
    @State var names = ["A","B","C","D"]
    @State private var name: String = "Ban Ki Moon"
    @State var placeholder: String = "Add Review"
    @State var value: CGFloat = 0
    init() {
        UITextView.appearance().backgroundColor = .clear
    }
    var body: some View {

        ZStack (alignment: .bottomLeading) {
            List {
                Section {
                    ForEach(names,id: \.self ) { name in
                        Group {
                            testStruct(name: name)
                        }
                    }.onDelete(perform: removeItems)
                }
                
                Section {
                    TextField("Enter your name",text: $name)
                    Text("Hello,\(name)!")
                }
               
                Section {
                        TextEditor(text: $placeholder)
                            .frame(height: 90 + 90)
                            .font(.body)
                            .foregroundColor(placeholder == "Add Review" ? .green : .gray)
                            .background(Color(UIColor.yellow))
                }
            }
        }.offset(y: -self.value/2)
        .animation(.spring())
        .onAppear {
            NotificationCenter.default.addobserver(forName: UIResponder.keyboardWillShowNotification,object: nil,queue: .main) {
                (notification) in
                let value = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
                let height = value.height
                self.value  = height
            }
            
            NotificationCenter.default.addobserver(forName: UIResponder.keyboardWillHideNotification,queue: .main) {
                (notification) in
                self.value  = 0
            }
        }
       
    }
    private func removeItems (indexSet: IndexSet) {
        names.remove(atOffsets: indexSet)
    }
}


struct testStruct: View,Identifiable {
    @State var name: String
    let id = UUID()
    var body: some View {
        HStack {
           Text(name)
           Spacer()
           Image(systemName: "folder.fill")
        }
    }
}

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

Image

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)