【问题标题】:Move TextView up with keyboard using Swift使用 Swift 使用键盘将 TextView 向上移动
【发布时间】:2018-07-19 20:18:36
【问题描述】:

我是 iOS 开发新手。我正在使用具有多个 TextField 和 TextView 的视图。我正在尝试向上移动视图以避免键盘隐藏编辑 TextField、TextView 内容。下面的代码适用于所有 TextField,但是当 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()
}

【问题讨论】:

标签: ios swift uiscrollview keyboard uikeyboard


【解决方案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
            }
        }
    }

【讨论】:

  • 您应该使用UIKeyboardFrameEndUserInfoKey 而不是UIKeyboardFrameBeginUserInfoKey。命名几乎可以解释自己,但为了进一步参考,您可以在这里查看:stackoverflow.com/questions/3332364/…
  • 在keyboardWillDisappear中你也可以设置self.view.frame.origin.y = 0 它也可以工作。
【解决方案2】:

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

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

【讨论】:

  • 谢谢......我有一些复杂的视图(嵌入式容器视图中的文本视图,......)......IQKeyboard 管理器没有按预期工作......
猜你喜欢
  • 2011-09-22
  • 2014-11-22
  • 2019-04-04
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多