Reputation: 717
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
}
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?
Upvotes: 1
Views: 1318
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
Reputation: 271
Here are the most common and used libraries for managing such behavior:
You can install them via CocoaPods as well.
Upvotes: 0
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