n s*_*ini 7 textfield ios swiftui
自从新的 Swift 版本以来,当我保存数据时,键盘似乎留下了空白。
我的看法
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()
}
}
Run Code Online (Sandbox Code Playgroud)
我在模拟器和 iPhone 11 (ios 14.3) 上有相同的行为
.resignKeyboardOnDragGesture ()是删除键盘的扩展。
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())
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这已经很晚了,但如果您仍然遇到这个问题。
将以下内容添加到您的视图中:
.ignoresSafeArea(.keyboard)
Run Code Online (Sandbox Code Playgroud)
这种情况不应该再发生。
| 归档时间: |
|
| 查看次数: |
825 次 |
| 最近记录: |