The app I'm creating is crashing whenever a button is pressed that is behind the keyboard.
I have the following view:
struct LoginSheetView: View {
// Variables Here
var body: some View {
SwiftyHUDView(isShowing: self.$isLoading) {
VStack{
Text("Login")
.font(.largeTitle)
.fontWeight(.semibold)
.padding(.bottom, 20)
TextField("Username", text: self.$username)
.padding(.bottom, 20)
.autocapitalization(.none)
.textContentType(UITextContentType.username)
SecureField("Password", text: self.$password)
.padding(.bottom, 20)
.textContentType(UITextContentType.password)
Button(action: {
self.isLoading = true
if(!self.username.isEmpty && !self.password.isEmpty){
self.databaseCalls.login(username: self.username, password: self.password){ b in
self.isLoading = false
print("is loading: \(self.isLoading)")
if(b){
self.loginStatus = true
self.presentationMode.wrappedValue.dismiss()
}else{
self.showingAlert = true
}
}
}
}){
Text("Login")
.frame(width: 220, height: 60)
.cornerRadius(15.0)
}
}
.alert(isPresented: self.$showingAlert) {
Alert(title: Text("Loging failed"), message: Text("Wrong username or password"), dismissButton: .default(Text("Got it!")))
}
.padding()
}
}
}
Which looks like this:
Whenever I press the button Login while the keyboard is up I get the following error in the file AppDelegate:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
This error does not happen if I press return on the keyboard then press the Login button.
Any ideas on what's causing the error ?
NOTE: I removed some code (variables declarations, padding, colors etc) to make it easier to digest

I've added:
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil)inside the button action and it has fixed the problem. Does anyone know if this is the correct way of approaching the issue ?