单击 tableView 内的文本字段时,ScrollView 没有向上移动 [英] ScrollView did not move up when click textfield inside tableView

查看:53
本文介绍了单击 tableView 内的文本字段时,ScrollView 没有向上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个代码:在 viewWillAppear 中:

I tried this code : in viewWillAppear :

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

和:

@objc func keyboardWillAppear(notification:NSNotification) {
    print("keyboard appear")
    var info = notification.userInfo
    let keyBoardSize = info![UIKeyboardFrameEndUserInfoKey] as! CGRect
    scrollCreateEditContact.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
    scrollCreateEditContact.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
}

@objc func keyboardWillDisappear(notification:NSNotification) {
    print("keyboard disappear")
    scrollCreateEditContact.contentInset = UIEdgeInsets.zero
    scrollCreateEditContact.scrollIndicatorInsets = UIEdgeInsets.zero
}

结果是:

我想要的是当键盘看起来像这样时,文本字段没有被键盘覆盖:

what I want is that textfield did not covered by keyboard when keyboard appear like this :

该代码仅适用于不在 tableView 内的文本字段.但是当我单击 tableView 中的文本字段并记录它时,总是检测到键盘出现".

That code only work on textfield that is not inside tableView. But when I click textfield inside tableView and log it "keyboard appear" always detected.

当键盘出现时,tableView 中没有被键盘覆盖的文本字段的正确代码是什么?

What is the correct code for textfield inside tableView not covered by keyboard when keyboard appear?

推荐答案

处理这个问题最简单的方法是为 IQKeyboardManager 安装 pod:

通过可可豆安装:

pod 'IQKeyboardManagerSwift'

用法:

import IQKeyboardManagerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

      IQKeyboardManager.shared.enable = true

      return true
    }
}

有关 IQKeyboardManager 的更多信息,请参阅此链接:

For more information on IQKeyboardManager refer this link:

https://github.com/hackiftekhar/IQKeyboardManager

这篇关于单击 tableView 内的文本字段时,ScrollView 没有向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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