使用Swift使用键盘向上移动TextView

时间:2018-02-09 05:44:18

标签: ios swift uiscrollview keyboard uikeyboard

我是iOS开发的新手。我正在使用多个TextField和TextView的视图。我正在尝试移动视图以避免键盘隐藏编辑TextField,TextView内容。下面的代码适用于所有TextFields但是当TextView是时它不会移动视图编辑。希望你能理解我的问题。 提前谢谢。

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self)
}  

 var keybordenabled = false
@objc func keyboardWillShow(notification: NSNotification) {
      if(keybordenabled == false){
    adjustInsetForKeyboardShow(show: true, notification: notification)
          keybordenabled = true
    } 
}

@objc func keyboardWillHide(notification: NSNotification) {
     keybordenabled = false

    adjustInsetForKeyboardShow(show: false, notification: notification)
}

func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
    let userInfo = notification.userInfo ?? [:]
    let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let adjustment = (keyboardFrame.height * (show ? 1 : -1)) + 20

    scrollView.contentInset.bottom += adjustment
    scrollView.scrollIndicatorInsets.bottom += adjustment
    self.view.layoutIfNeeded()
}



func textViewDidBeginEditing(_ textView: UITextView)
{
    if (textView.text == "Enter comment here...")
    {
        textView.text = ""
        textView.textColor = .black
    }
    textView.becomeFirstResponder() 
}

func textViewDidEndEditing(_ textView: UITextView)
{
    if (textView.text == "")
    {
        textView.text = "Enter comment here..."
        textView.textColor = .lightGray
    }
    textView.resignFirstResponder()
}

2 个答案:

答案 0 :(得分:1)

This is  work Me!

override func viewWillAppear(_ animated: Bool) {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil)
    }


override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self)
    }


 @objc func keyboardWillAppear(_ notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0{
                self.view.frame.origin.y -= keyboardSize.height
            }
        }
    }

 @objc func keyboardWillDisappear(_ notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y != 0{
                self.view.frame.origin.y += keyboardSize.height
            }
        }
    }

答案 1 :(得分:0)

@Jhon您可以使用IQKeyboard第三方库来处理文本字段和文本视图滚动键盘的外观。使用此 IQKeyboard SDK将管理所有文本字段和textView的自动滚动,同时还可以节省您的工作和时间。

请参阅此链接:https://github.com/hackiftekhar/IQKeyboardManager