当键盘出现时,选择导致视图向上滑动的原因 [英] When Keyboard Present, Choose What Causes View To Slide Up

查看:32
本文介绍了当键盘出现时,选择导致视图向上滑动的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tableView,第 0 行中有一个 textField,第 3 行中有一个 textView.我的 tableView 当前每次在键盘出现时都会向上滑动.当 tableView 向上滑动时,您看不到第 0 行内的 textField.如何为第 0 行禁用此功能并仅保留第 3 行?我尝试使用使用协议 &委托尝试仅针对第 3 行封装该函数,但这不起作用.

I have a tableView with a textField within row 0 and textView within row 3. My tableview currently slides up every time when the keyboard is present. When the tableView slides up, you can't see the textField within row 0. How do I disable this for row 0 and just keep for row 3? I tried using using Protocol & Delegates to try to encapsulate the function only for row 3, but that doesn't work.

类 CreateEditItemController: UIViewController, CreateItemDescriptionCellDelegate {

class 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 控制器上手动执行此操作.

What you are trying to do is possible after some mathematics but i would recommend using a third party pod for this instead of doing this manually on evert controller.

将此添加到您的 pod 文件中:

Add this to your pod file:

# 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

For further detail and documentation view: https://github.com/hackiftekhar/IQKeyboardManager

你必须写的唯一一行是:

The only line you would have to write would be :

// Enabling IQKeyboardManager
IQKeyboardManager.shared.enable = true

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

这将解决您的所有问题,您无需计算帧数或其他任何东西.

This will solve all your problems and you wont have to calculate the frames or anything.

这篇关于当键盘出现时,选择导致视图向上滑动的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆