问题
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:
- TPKeyboardAvoiding
- IQKeyboardManager
You can install them via CocoaPods as well.
来源:https://stackoverflow.com/questions/54283069/scrollview-did-not-move-up-when-click-textfield-inside-tableview