Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 当键盘仅出现在小型设备上时,如何移动视图_Swift_Xcode_Keyboard - Fatal编程技术网

Swift 当键盘仅出现在小型设备上时,如何移动视图

Swift 当键盘仅出现在小型设备上时,如何移动视图,swift,xcode,keyboard,Swift,Xcode,Keyboard,我正在开发iPhone应用程序,我有多个UITextFields用于输入 只有像iphone5、6这样的小型设备才有问题。当键盘出现时,底部文本字段隐藏。 它可以很好地与像XR、XS Max这样的iPhone大屏幕配合使用 如何添加检查底部文本字段是否隐藏的条件 guard let keyboardReact = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue

我正在开发iPhone应用程序,我有多个UITextFields用于输入

只有像iphone5、6这样的小型设备才有问题。当键盘出现时,底部文本字段隐藏。 它可以很好地与像XR、XS Max这样的iPhone大屏幕配合使用

如何添加检查底部文本字段是否隐藏的条件

 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
                }
            }
        }

这适用于所有屏幕大小。我希望它仅在键盘隐藏文本字段时才起作用。现在,您可以计算键盘高度并相应地移动视图

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
         }
      }
 }
}
或者你可以试试这个第三方库

你可能会得到答案