Reputation:
I made an iOS app with Xcode and Swift. In one ViewController there's a form with multiple UITextFields. My problem is, that the TextFields near the bottom are hidden behind the keyboard when it is opened.
I already found a workaround that moves the whole View up as soon as the keyboard appears. But that means, that the topmost TextFields get hidden.
I'm already spending hours to find a working solution, but without success.
Is there anybody who can help me please?
EDIT
I decided to try IQKeyboardManager
library.
Since I don't use CocoaPods I tried the "manual" way.
I added this line of code (IQKeyboardManager.sharedManager().enable = true
) in didFinishLaunchingWithOptions
in AppDelegate and I moved (drag and drop) the whole IQKeyboardManagerSwift
folder in the left side bar in Xcode.
But I'm getting: Use of unresolved identifier 'IQKeyboardManager'
.
What am I doing wrong?
Upvotes: 2
Views: 2683
Reputation:
That worked for me, though I would like to try IQKeyboardManager
: http://glaucocustodio.com/2015/09/26/content-locked-under-keyboard-another-approach-to-solve/
Upvotes: -3
Reputation: 2802
You can use UITableViewController and get the behavior you want for free. Just put a text field in a cell and it will automatically scroll the text field so the keyboard won't hide it.
Upvotes: 0
Reputation: 3383
Try This ->
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == nameTxtField {
self.view.frame = CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height)
}
if textField == EmailTxtField {
self.view.frame = CGRectMake(0, -100, self.view.frame.size.width, self.view.frame.size.height)
}
return true
}
Upvotes: 0
Reputation: 76
Unfortunately I cannot comment due to my reputation, so I comment here. Let me understand, you want to move the view up only if you are going to edit a UITextField that when hit is going to be covered by the keyboard, right?
If the answer is yes, when the keyboard is going to show, check who is the first responder with something like
if bottomTextField.isFirstResponder() {
view.frame.origin.y = getKeyboardHeight() * -1
}
Since you know which text fields are going to be covered by keyboard, you can slide the view up only if one of them is being edited. You can get the keyboard height from UIKeyboardWillShowNotification. If you are interested on this approach, comment and I will edit adding some code to accomplish the whole thing
Upvotes: 0
Reputation: 11539
You should consider putting the text fields in a scroll view and when the keyboard appears, simply adjust the height of the scroll view by the keyboard height. This will keep the entire scroll view on screen and allow you to scroll through the text fields.
Upvotes: 0
Reputation: 2459
For all these UITextField
+ Keyboard issues, I highly recommend you to use IQKeyboardManager library. It will handle the problem without a line of code.
If you want to do things by yourself, you should have a look at this answer
Upvotes: 2