带有 TextField 的 SwiftUI 列表在键盘时调整 appears/disappears
SwiftUI List with TextField adjust when keyboard appears/disappears
我用 SwiftUI 写了一个列表。我还有一个用作搜索栏的 TextField 对象。我的代码如下所示:
import SwiftUI
struct MyListView: View {
@ObservedObject var viewModel: MyViewModel
@State private var query = ""
var body: some View {
NavigationView {
List {
// how to listen for changes here?
// if I add onEditingChange here, Get the value only after the user finish search (by pressing enter on the keyboard)
TextField(String.localizedString(forKey: "search_bar_hint"), text: self.$query) {
self.fetchListing()
}
ForEach(viewModel.myArray, id: \.id) { arrayObject in
NavigationLink(destination: MyDetailView(MyDetailViewModel(arrayObj: arrayObject))) {
MyRow(arrayObj: arrayObject)
}
}
}
.navigationBarTitle(navigationBarTitle())
}
.onAppear(perform: fetchListing)
}
private func fetchListing() {
query.isEmpty ? viewModel.fetchRequest(for: nil) : viewModel.fetchRequest(for: query)
}
private func navigationBarTitle() -> String {
return query.isEmpty ? String.localizedString(forKey: "my_title") : query
}
}
我现在遇到的问题是列表仍然在键盘后面:(。我如何设置列表填充底部或边缘插入(或其他任何有效的方法,我完全打开)以便列表的滚动在键盘上方结束?列表“大小”也应该根据键盘是打开还是关闭自动调整。
问题看起来像这样:
请就此提供任何建议,我真的不知道该怎么做:(。我是一个 SwiftUI 初学者,正在尝试学习它:)。
您可以试试下面的方法,自己添加详细的动画。
@ObservedObject var keyboard = KeyboardResponder()
var body: some View {
NavigationView {
List {
// how to listen for changes here?
// if I add onEditingChange here, Get the value only after the user finish search (by pressing enter on the keyboard)
TextField("search_bar_hint", text: self.$query) {
self.fetchListing()
}
ForEach(self.viewModel, id: \.self) { arrayObject in
Text(arrayObject)
}
}.padding(.bottom, self.keyboard.currentHeight).animation(.easeIn(duration: self.keyboard.keyboardDuration))
.navigationBarTitle(self.navigationBarTitle())
}
.onAppear(perform: fetchListing)
}
class KeyboardResponder: ObservableObject {
@Published var currentHeight: CGFloat = 0
@Published var keyboardDuration: TimeInterval = 0
private var anyCancellable: Set<AnyCancellable> = Set<AnyCancellable>()
init() {
let publisher1 = NotificationCenter.Publisher(center: .default, name: UIResponder.keyboardWillShowNotification).map{ notification -> Just<(CGFloat, TimeInterval)> in
guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {return Just((CGFloat(0.0), 0.0)) }
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return Just((CGFloat(0.0), 0.0)) }
return Just((keyboardSize.height, duration))}
let publisher2 = NotificationCenter.Publisher(center: .default, name: UIResponder.keyboardWillHideNotification) .map{ notification -> Just<(CGFloat, TimeInterval)> in
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return Just((CGFloat(0.0), 0.0)) }
return Just((0.0, duration))}
Publishers.Merge(publisher1, publisher2).switchToLatest().subscribe(on: RunLoop.main).sink(receiveValue: {
if [=10=].1 > 1e-6 { self.currentHeight = [=10=].0 }
self.keyboardDuration = [=10=].1
}).store(in: &anyCancellable)
}
}
键盘填充问题的解决方案与 E.coms 建议的一样。也可以使用kontiki这里写的class:
我遇到的问题是由于引用类型的多个实例发布类似的状态更改,导致我的视图层次结构中的状态更改。
我的视图模型是引用类型,它发布对其模型的更改,模型是值类型。但是,这些视图模型还包含处理网络请求的引用类型。对于我渲染的每个视图(每一行),我都分配了一个新的视图模型实例,这也创建了一个新的网络服务实例。继续这种模式,这些网络服务中的每一个也创建和分配新的网络管理器。
我用 SwiftUI 写了一个列表。我还有一个用作搜索栏的 TextField 对象。我的代码如下所示:
import SwiftUI
struct MyListView: View {
@ObservedObject var viewModel: MyViewModel
@State private var query = ""
var body: some View {
NavigationView {
List {
// how to listen for changes here?
// if I add onEditingChange here, Get the value only after the user finish search (by pressing enter on the keyboard)
TextField(String.localizedString(forKey: "search_bar_hint"), text: self.$query) {
self.fetchListing()
}
ForEach(viewModel.myArray, id: \.id) { arrayObject in
NavigationLink(destination: MyDetailView(MyDetailViewModel(arrayObj: arrayObject))) {
MyRow(arrayObj: arrayObject)
}
}
}
.navigationBarTitle(navigationBarTitle())
}
.onAppear(perform: fetchListing)
}
private func fetchListing() {
query.isEmpty ? viewModel.fetchRequest(for: nil) : viewModel.fetchRequest(for: query)
}
private func navigationBarTitle() -> String {
return query.isEmpty ? String.localizedString(forKey: "my_title") : query
}
}
我现在遇到的问题是列表仍然在键盘后面:(。我如何设置列表填充底部或边缘插入(或其他任何有效的方法,我完全打开)以便列表的滚动在键盘上方结束?列表“大小”也应该根据键盘是打开还是关闭自动调整。
问题看起来像这样:
请就此提供任何建议,我真的不知道该怎么做:(。我是一个 SwiftUI 初学者,正在尝试学习它:)。
您可以试试下面的方法,自己添加详细的动画。
@ObservedObject var keyboard = KeyboardResponder()
var body: some View {
NavigationView {
List {
// how to listen for changes here?
// if I add onEditingChange here, Get the value only after the user finish search (by pressing enter on the keyboard)
TextField("search_bar_hint", text: self.$query) {
self.fetchListing()
}
ForEach(self.viewModel, id: \.self) { arrayObject in
Text(arrayObject)
}
}.padding(.bottom, self.keyboard.currentHeight).animation(.easeIn(duration: self.keyboard.keyboardDuration))
.navigationBarTitle(self.navigationBarTitle())
}
.onAppear(perform: fetchListing)
}
class KeyboardResponder: ObservableObject {
@Published var currentHeight: CGFloat = 0
@Published var keyboardDuration: TimeInterval = 0
private var anyCancellable: Set<AnyCancellable> = Set<AnyCancellable>()
init() {
let publisher1 = NotificationCenter.Publisher(center: .default, name: UIResponder.keyboardWillShowNotification).map{ notification -> Just<(CGFloat, TimeInterval)> in
guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {return Just((CGFloat(0.0), 0.0)) }
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return Just((CGFloat(0.0), 0.0)) }
return Just((keyboardSize.height, duration))}
let publisher2 = NotificationCenter.Publisher(center: .default, name: UIResponder.keyboardWillHideNotification) .map{ notification -> Just<(CGFloat, TimeInterval)> in
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return Just((CGFloat(0.0), 0.0)) }
return Just((0.0, duration))}
Publishers.Merge(publisher1, publisher2).switchToLatest().subscribe(on: RunLoop.main).sink(receiveValue: {
if [=10=].1 > 1e-6 { self.currentHeight = [=10=].0 }
self.keyboardDuration = [=10=].1
}).store(in: &anyCancellable)
}
}
键盘填充问题的解决方案与 E.coms 建议的一样。也可以使用kontiki这里写的class:
我遇到的问题是由于引用类型的多个实例发布类似的状态更改,导致我的视图层次结构中的状态更改。
我的视图模型是引用类型,它发布对其模型的更改,模型是值类型。但是,这些视图模型还包含处理网络请求的引用类型。对于我渲染的每个视图(每一行),我都分配了一个新的视图模型实例,这也创建了一个新的网络服务实例。继续这种模式,这些网络服务中的每一个也创建和分配新的网络管理器。