我有一个tableView,第0行中有一个textField,第3行中有一个textView。每次键盘出现时,我的tableview都会向上滑动。当tableView向上滑动时,您无法在第0行中看到文本字段。如何对第0行禁用此项,并仅保留第3行?我尝试使用Protocol&Delegates来尝试只为第3行封装函数,但这不起作用。
swift - 存在键盘时,选择导致 View 向上滑动的原因-LMLPHP
类CreateEditItemController:UIViewController,CreateItemDescriptionCellDelegate{

@IBOutlet weak var tableView: UITableView!

func handleKeyboardShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

        if self.view.frame.origin.y == 0 {
            //self.view.frame.origin.y -= keyboardSize.height
            self.view.frame.origin.y -= 200
        }
    }
}

func handleKeyboardHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    ...
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateItemDescriptionCell", for: indexPath) as! CreateItemDescriptionCell
        cell.delegate = self
        return cell
    default:
        return tableView.cellForRow(at: indexPath)!
     }
   }
 }


protocol CreateItemDescriptionCellDelegate: class {
    func handleKeyboardShow(notification: NSNotification)
    func handleKeyboardHide(notification: NSNotification)
}

class CreateItemDescriptionCell: UITableViewCell, UITextViewDelegate {
//IBOUTLETS
@IBOutlet weak var notesTextView: UITextView!
weak var delegate: CreateItemDescriptionCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    notesTextView.delegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

@objc func handleKeyboardShow(notification: NSNotification) {
    delegate?.handleKeyboardShow(notification: notification)
}

@objc func handleKeyboardHide(notification: NSNotification) {
    delegate?.handleKeyboardHide(notification: notification)
 }
}

最佳答案

在经过一些数学运算之后,你所要做的是可能的,但是我建议使用第三方pod来完成这项工作,而不是在evert控制器上手动完成这项工作。
将此添加到pod文件:

# IQKeyboardManager: Codeless drop-in universal library allows to prevent issues of keyboard sliding up
# https://github.com/hackiftekhar/IQKeyboardManager
pod 'IQKeyboardManagerSwift'

有关详细信息和文档视图:
https://github.com/hackiftekhar/IQKeyboardManager
你只需要写一行:
// Enabling IQKeyboardManager
IQKeyboardManager.shared.enable = true

在里面
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

这将解决你的所有问题,你不必计算框架或任何东西。

关于swift - 存在键盘时,选择导致 View 向上滑动的原因,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55561143/

10-10 08:43