键盘出现时向上移动UITextField

时间:2017-11-18 11:15:50

标签: ios swift uitextfield

我正在尝试编写代码以确保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
}





 }

2 个答案:

答案 0 :(得分:0)

试试此代码

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

答案 1 :(得分:0)

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

{

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

}