ScrollView did not move up when click textfield inside tableView

不想你离开。 提交于 2019-12-13 00:29:35

问题


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)

and :

@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
}

and result is :

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

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.

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


回答1:


The easiest way to handle this problem is installing pod for IQKeyboardManager:

Installation via cocoa pods:

pod 'IQKeyboardManagerSwift'

Usage:

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

For more information on IQKeyboardManager refer this link:

https://github.com/hackiftekhar/IQKeyboardManager




回答2:


Thats common behaviour, ios cannot adjust content above keyboard automatically, unlike android. My solution is, you can wrap all that views (photo, textfield, etc) inside tableView. And use TPKeyboardAvoiding library.

pod 'TPKeyboardAvoiding'

If you use storyboard, set tableView base-class to TPKeyboardAvoidingTableView.




回答3:


Here are the most common and used libraries for managing such behavior:

  1. TPKeyboardAvoiding
  2. IQKeyboardManager

You can install them via CocoaPods as well.



来源:https://stackoverflow.com/questions/54283069/scrollview-did-not-move-up-when-click-textfield-inside-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!