我尝试了这段代码:
在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
}

结果是:
ios - 单击tableView内的文本字段时,ScrollView没有向上移动-LMLPHP

我想要的是当键盘出现时,文本字段没有被键盘覆盖:

ios - 单击tableView内的文本字段时,ScrollView没有向上移动-LMLPHP

该代码仅适用于不在tableView内的文本字段。
但是,当我单击tableView内的文本字段并将其记录下来时,始终会检测到“键盘出现”。

出现键盘时,TableView内部文本字段的正确代码是键盘未覆盖的正确代码是什么?

最佳答案

处理此问题的最简单方法是为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的更多信息,请参考以下链接:

https://github.com/hackiftekhar/IQKeyboardManager

关于ios - 单击tableView内的文本字段时,ScrollView没有向上移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54283069/

10-13 08:56