英文:
How to exclude TextField tap while hiding keyboard
问题
目前,我在用户点击任何地方时都会隐藏键盘。我希望排除两个TextField
的点击以避免键盘的隐藏和显示效果。
我想要:
- 对于我的
TextField
,希望有一个稳定的键盘,没有隐藏和显示的效果。 - 点击任何地方来关闭/隐藏键盘。
请查看以下效果:
英文:
Currently, I am hiding the keyboard when the user taps anywhere. I want to exclude 2 TextField
s tap to avoid the keyboard hide/show effect.
import SwiftUI
struct ContentView: View {
@FocusState var focus:FocusedField?
@State var name = ""
@State var email = ""
@State var phoneNumber = ""
var body: some View {
List {
TextField("Name:",text: $name)
.focused($focus, equals: .name)
.onSubmit {
focus = .email
}
TextField("Email:",text: $email)
.focused($focus, equals: .email)
.onSubmit {
focus = .phone
}
TextField("PhoneNumber:", text: $phoneNumber)
.focused($focus, equals: .phone)
.onSubmit {
if !name.isEmpty && !email.isEmpty && !phoneNumber.isEmpty {
submit()
}
}
}
.onTapGesture {
if (focus != nil) {
hideKeyboard()
}
}
}
private func submit() {
print("submit")
}
enum FocusedField: Hashable {
case name, email, phone
}
}
extension View {
func hideKeyboard() {
print("hideKeyboard")
let resign = #selector(UIResponder.resignFirstResponder)
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
}
}
I want:
- A stable keyboard for my
TextField
s with no hide and show effects. - Tap anywhere to dismiss/hide the keyboard.
Please see the following effects:
答案1
得分: 0
有一个在iOS 16及以后可用的键盘隐藏修饰符:
func scrollDismissesKeyboard(_ mode: ScrollDismissesKeyboardMode) -> some View
您可能希望使用 immediately
模式。
var body: some View{
List {
// 等等。
}
.scrollDismissesKeyboard(.immediately)
}
这不是在点击时隐藏键盘,而是在滚动时隐藏,这是典型的iOS行为。
英文:
There is a modifier that defines keyboard dismissal available since iOS 16:
func scrollDismissesKeyboard(_ mode: ScrollDismissesKeyboardMode) -> some View
You probably want the immediately
mode.
var body: some View{
List {
// etc.
}
.scrollDismissesKeyboard(.immediately)
}
This does not dismiss the keyboard on tap but on scroll which is the typical iOS behaviour.
答案2
得分: 0
I found the answer.
public struct TapToHideKeyboardView: View {
public init() {
}
public var body: some View {
Button(action: {
self.hideKeyboard()
}) {
Rectangle().foregroundColor(.brown)
}
.background(
GeometryReader { geometry in
Color.clear
.frame(width: geometry.size.width, height: geometry.size.height)
}
.onTapGesture {
self.hideKeyboard()
}
)
}
func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
Use this public struct inside Zstack.
var body: some View{
NavigationStack{
ZStack {
TapToHideKeyboardView()
VStack{
TextField("Name:",text:$name)
.focused($focus, equals: .name)
.onSubmit {
focus = .email
}
TextField("Email:",text:$email)
.focused($focus,equals: .email)
.onSubmit {
focus = .phone
}
TextField("PhoneNumber:",text:$phoneNumber)
.focused($focus, equals: .phone)
.onSubmit {
if !name.isEmpty && !email.isEmpty && !phoneNumber.isEmpty {
submit()
}
}
}
}
}
}
英文:
I found the answer.
public struct TapToHideKeyboardView: View {
public init() {
}
public var body: some View {
Button(action: {
self.hideKeyboard()
}) {
Rectangle().foregroundColor(.brown)
}
.background(
GeometryReader { geometry in
Color.clear
.frame(width: geometry.size.width, height: geometry.size.height)
}
.onTapGesture {
self.hideKeyboard()
}
)
}
func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
Use this public struct inside Zstack.
var body: some View{
NavigationStack{
ZStack {
TapToHideKeyboardView()
VStack{
TextField("Name:",text:$name)
.focused($focus, equals: .name)
.onSubmit {
focus = .email
}
TextField("Email:",text:$email)
.focused($focus,equals: .email)
.onSubmit {
focus = .phone
}
TextField("PhoneNumber:",text:$phoneNumber)
.focused($focus, equals: .phone)
.onSubmit {
if !name.isEmpty && !email.isEmpty && !phoneNumber.isEmpty {
submit()
}
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论