2

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?

  • I used this [solution](http://stackoverflow.com/questions/25048941/move-screen-when-keyboard-appears-in-swift) and work correctly. – Ahmed Abdallah Aug 23 '16 at 14:51

6 Answers6

2

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

Community
  • 1
  • 1
Julien Quere
  • 2,407
  • 16
  • 21
  • Will I need CocoaPod for this library? –  Aug 23 '16 at 14:52
  • You can use Cocoapod to integrate this library (that's the easiest way). But you can also integrate it manually as explain in the [README](https://github.com/hackiftekhar/IQKeyboardManager#source-code-method-) – Julien Quere Aug 23 '16 at 14:54
  • I'm trying the manual version. 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? –  Aug 23 '16 at 15:08
  • Great discovery! – Nathan Barreto Dec 19 '17 at 18:16
0

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

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.

Callam
  • 11,409
  • 2
  • 34
  • 32
0

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

0

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.

Oleg Sherman
  • 2,772
  • 1
  • 21
  • 20
-3

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/

  • Doesnt' wor well - it seems to interfere with other pops such as those that add UI changes such as autocomplete fields – UKDataGeek Jul 15 '17 at 23:59