gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:55:34 26 4
gpt4 key购买 nike

我是 iOS 开发新手。我正在使用具有多个 TextField 和 TextView 的 View 。我正在尝试向上移动 View 以避免键盘隐藏编辑 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()
}

最佳答案

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

关于ios - 使用 Swift 使用键盘向上移动 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com