-1

I am working on iPhone application and I have multiple UITextFields for input.

the problem with small devices only like iPhone 5 , 6. when The keyboard appears , the bottom textFields hide. it is working fine with big iPhone screen like XR , XS Max

how can I add condition that check if the bottom textfields Hidden or not ?

 guard let keyboardReact = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
            return
        }
        let screen = view.frame.size.height - keyboardReact.height
        let safeAreHeight = self.view.frame.height - self.topLayoutGuide.length - self.bottomLayoutGuide.length

        if  safeAreHeight + keyboardReact.height > view.frame.size.height {
            if currentTappedTextField == phoneTextField || currentTappedTextField == employeeEmailTextField  || currentTappedTextField == relationTextField {
                if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification{
                    view.frame.origin.y = -(keyboardReact.height)
                } else {
                    view.frame.origin.y = 0
                }
            }
        }

This is work with all screen sizes I want it work only when the keyboard hide the textFields

a_aw
  • 11
  • 6

1 Answers1

0

Now you can calculate your keyboard height and move your view accodingly

func liftViewUp(notification: NSNotification){
        if let keyboardSize = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect {
       // manage your view accordingly here



 if currentTappedTextField == phoneTextField || currentTappedTextField == employeeEmailTextField  || currentTappedTextField == relationTextField {
                    if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification{



     let textFieldPosition = currentTappedTextField.frame.origin.y + currentTappedTextField.frame.size.height

// check if textfield will hide behind keyboard
  if textFieldPosition > (view.frame.size.height - keyboardReact.height){

            view.frame.origin.y = -(keyboardReact.height)
     }else {
            view.frame.origin.y = 0
           }
 } else {
          view.frame.origin.y = 0
         }
      }
 }
}

or You can try this third party library IQKeyboardManager

You may got answer here

Abhi Yaduwanshi
  • 380
  • 3
  • 12