出现键盘时向上移动 UITextField

Move UITextField up when keyboard appears

我正在尝试编写代码以确保 UITextField 在键盘出现时向上移动。我已经根据在堆栈溢出中发现的内容编写了代码,但是它不起作用。原来的代码是用objective-c写的,但我不熟悉它,所以决定用swift写代码。

谁能告诉我我在这里干什么?

 {

        // moving text box up when tyoing
func registerForKeyboardNotifications()
{
    //Adding notifies on keyboard appearing
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}



@objc func keyboardWasShown(notification: NSNotification)
{
    //Need to calculate keyboard exact size due to Apple suggestions

    self.scrollView.isScrollEnabled = true
    let info : NSDictionary = notification.userInfo! as NSDictionary
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets

    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize!.height
    if (yourEmail) != nil
    {
        if (!aRect.contains(yourEmail!.frame.origin))
        {
            self.scrollView.scrollRectToVisible(yourEmail!.frame, animated: true)
        }
    }


}


@objc func keyboardWillBeHidden(notification: NSNotification)
{
    //Once keyboard disappears, restore original positions
    let info : NSDictionary = notification.userInfo! as NSDictionary as NSDictionary
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    self.view.endEditing(true)
    self.scrollView.isScrollEnabled = false

}

func textFieldDidBeginEditing(_ textField: UITextField)
{
    yourEmail = textField
}

func textFieldDidEndEditing(_ textField: UITextField)
{
    yourEmail = nil
}





 }

试试这个代码

func textFieldDidBeginEditing(textField: UITextField!)
{
     self.scrollView.setContentOffset(CGPoint.init(x: 0, y: scrollBy), animated: true)
  // scrollBy - pass the height you want your scrollview to be scrolled when keyboard appears
 }

func textFieldDidEndEditing(textField: UITextField!)
{
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

    self.view.endEditing(true);
}

下面是我在建议的帮助下编写的代码:

{

func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField.frame.maxY > self.view.frame.height * 0.6
    {
        self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - self.view.frame.height * 0.6 + 2.0), animated: true)

    }
    else{
        return
    }

}

func textFieldDidEndEditing(_ textField: UITextField)
{
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

    self.view.endEditing(true);
}

}