Anonymous
Клавиатура iPhone исчезает и появляется снова при переключении между полями с помощью onTapGesture(hideKeyboard)
Сообщение
Anonymous » 03 июл 2024, 09:53
У меня есть этот код в Swift (iOS 16+), и у меня есть экран входа в мое приложение, где пользователю необходимо ввести адрес электронной почты и пароль.
У меня также есть .onTapGesture в GeometryReader { ... , объединяющем все воедино. Я хочу, чтобы пользователь мог отключить клавиатуру при нажатии вне текстового поля.
Но когда пользователь вводит адрес электронной почты, а затем хочет ввести пароль, клавиатура исчезает, а затем появляется снова. Как мне это сделать, чтобы клавиатура оставалась открытой (ожидаемый результат тот же, что и без onTapGesture).
Вот мой код
Код: Выделить всё
struct LoginView: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@State var email: String = ""
@State var password: String = ""
private let hapticFeedbackService = HapticFeedbackService()
var body: some View {
GeometryReader { screenGeometry in
let screenHeight = screenGeometry.size.height
ZStack {
Color.black.background.ignoresSafeArea(.all)
VStack {
WelcomeTextView(welcomeText: "sign_in_to")
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.top, screenHeight * 0.02)
VStack {
InputField(
text: $email,
placeholder: "enter_email",
isSecure: false
)
.padding(.bottom, 15)
InputField(
text: $password,
placeholder: "enter_password",
isSecure: true,
trailingIcon: Image("Lock")
)
}
Button(action: {
hapticFeedbackService.triggerHapticFeedback()
print("Button pressed")
}) {
Text(LocalizedStringKey("forgot_your_password"))
.font(.thinnerSmallTitle)
}
.padding(.top, 5)
.padding(.trailing, 5)
.frame(maxWidth: .infinity, alignment: .trailing)
LargeButton(
text: "sign_in",
textColor: Color.black,
backgroundColor: Color.white
) {
hapticFeedbackService.triggerHapticFeedback()
print("Button pressed")
}
.padding(.top, 40)
Text(LocalizedStringKey("other_login_options"))
.font(.mediumText)
.padding(.top, 10)
HStack {
SSOButton(type: .Apple, action: {
hapticFeedbackService.triggerHapticFeedback()
print("Button pressed")
})
.padding(.trailing, 5)
SSOButton(type: .Google, action: {
hapticFeedbackService.triggerHapticFeedback()
print("Button pressed")
})
.padding(.leading, 5)
}
.padding(.top, 10)
HStack {
Text(LocalizedStringKey("dont_have_account"))
.font(.mediumText)
.foregroundColor(Color.black)
Button(action: {
hapticFeedbackService.triggerHapticFeedback()
print("Button pressed")
}) {
Text(LocalizedStringKey("dont_have_account_sign_up"))
.font(.smallTitle)
.foregroundColor(Color.black)
}
}
.padding(.top, 25)
Spacer()
}
.paddedFrame(
screenSize: screenGeometry.size,
horizontalSizeClass: horizontalSizeClass
)
}
}
.ignoresSafeArea(.keyboard)
.onTapGesture { self.hideKeyboard() }
}
private func hideKeyboard() {
// UIApplication.shared.keyWindow?.endEditing(true) - Depricated
UIApplication.shared.sendAction(
#selector(UIResponder.resignFirstResponder),
to: nil,
from: nil,
for: nil
)
}
}
Спасибо.
Подробнее здесь:
https://stackoverflow.com/questions/787 ... ds-with-on
1719989636
Anonymous
У меня есть этот код в Swift (iOS 16+), и у меня есть экран входа в мое приложение, где пользователю необходимо ввести адрес электронной почты и пароль. У меня также есть .onTapGesture в GeometryReader { ... , объединяющем все воедино. Я хочу, чтобы пользователь мог отключить клавиатуру при нажатии вне текстового поля. Но когда пользователь вводит адрес электронной почты, а затем хочет ввести пароль, клавиатура исчезает, а затем появляется снова. Как мне это сделать, чтобы клавиатура оставалась открытой (ожидаемый результат тот же, что и без onTapGesture). Вот мой код [code]struct LoginView: View { @Environment(\.horizontalSizeClass) var horizontalSizeClass @State var email: String = "" @State var password: String = "" private let hapticFeedbackService = HapticFeedbackService() var body: some View { GeometryReader { screenGeometry in let screenHeight = screenGeometry.size.height ZStack { Color.black.background.ignoresSafeArea(.all) VStack { WelcomeTextView(welcomeText: "sign_in_to") .frame(maxWidth: .infinity, alignment: .leading) .padding(.top, screenHeight * 0.02) VStack { InputField( text: $email, placeholder: "enter_email", isSecure: false ) .padding(.bottom, 15) InputField( text: $password, placeholder: "enter_password", isSecure: true, trailingIcon: Image("Lock") ) } Button(action: { hapticFeedbackService.triggerHapticFeedback() print("Button pressed") }) { Text(LocalizedStringKey("forgot_your_password")) .font(.thinnerSmallTitle) } .padding(.top, 5) .padding(.trailing, 5) .frame(maxWidth: .infinity, alignment: .trailing) LargeButton( text: "sign_in", textColor: Color.black, backgroundColor: Color.white ) { hapticFeedbackService.triggerHapticFeedback() print("Button pressed") } .padding(.top, 40) Text(LocalizedStringKey("other_login_options")) .font(.mediumText) .padding(.top, 10) HStack { SSOButton(type: .Apple, action: { hapticFeedbackService.triggerHapticFeedback() print("Button pressed") }) .padding(.trailing, 5) SSOButton(type: .Google, action: { hapticFeedbackService.triggerHapticFeedback() print("Button pressed") }) .padding(.leading, 5) } .padding(.top, 10) HStack { Text(LocalizedStringKey("dont_have_account")) .font(.mediumText) .foregroundColor(Color.black) Button(action: { hapticFeedbackService.triggerHapticFeedback() print("Button pressed") }) { Text(LocalizedStringKey("dont_have_account_sign_up")) .font(.smallTitle) .foregroundColor(Color.black) } } .padding(.top, 25) Spacer() } .paddedFrame( screenSize: screenGeometry.size, horizontalSizeClass: horizontalSizeClass ) } } .ignoresSafeArea(.keyboard) .onTapGesture { self.hideKeyboard() } } private func hideKeyboard() { // UIApplication.shared.keyWindow?.endEditing(true) - Depricated UIApplication.shared.sendAction( #selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil ) } } [/code] Спасибо. Подробнее здесь: [url]https://stackoverflow.com/questions/78700421/iphone-keyboard-dissapear-and-appear-again-when-switching-between-fields-with-on[/url]