【问题标题】:When Keyboard Present, Choose What Causes View To Slide Up当键盘出现时,选择导致视图向上滑动的原因
【发布时间】:2019-04-07 16:20:06
【问题描述】:

我有一个 tableView,第 0 行有一个 textField,第 3 行有 textView。我的 tableview 当前每次有键盘时都会向上滑动。当 tableView 向上滑动时,您看不到第 0 行内的 textField。如何为第 0 行禁用此功能而只保留第 3 行?我尝试使用 Protocol & Delegates 尝试仅为第 3 行封装函数,但这不起作用。

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

【问题讨论】:

    标签: swift user-interface uikit


    【解决方案1】:

    经过一些数学运算后,您尝试做的事情是可能的,但我建议为此使用第三方 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 
    

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

    【讨论】:

      【解决方案2】:

      有几条路线可供您使用,但为了简单起见,您可以使用一些库来为您完成所有计算,您无需担心。

      我一直使用的一个是:

      TPKeyboardAvoiding - https://github.com/michaeltyson/TPKeyboardAvoiding

      IQKeyboardManagerSwift - https://github.com/hackiftekhar/IQKeyboardManager

      还有很多其他的。

      【讨论】:

        猜你喜欢
        • 2018-05-21
        • 2014-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多