Sarimin
Sarimin

Reputation: 717

ScrollView did not move up when click textfield inside tableView

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 : enter image description here

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

enter image description here

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?

Upvotes: 1

Views: 1318

Answers (3)

Rahul Umap
Rahul Umap

Reputation: 2869

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

Upvotes: 2

Kushal Panchal
Kushal Panchal

Reputation: 271

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

  1. TPKeyboardAvoiding
  2. IQKeyboardManager

You can install them via CocoaPods as well.

Upvotes: 0

axunic
axunic

Reputation: 2316

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.

Upvotes: 3

Related Questions