0

i have developed on im app. when the user taps the text field the keyboard pops up and the table view containing the messages moves up but the top of the table view is un viewable as it is off the screen. it is also having the undesired effect of turning the screen black when the user moves the keyboard suggestions up or down

override func viewWillAppear(animated:Bool) {
    super.viewWillAppear(animated)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}



func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height
            self.animateTextField(true)
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    self.animateTextField(false)
}

func animateTextField(up: Bool) {
    let movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.2, animations: {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
    })
}

How can i control the display of the tableview, text field and keyboard?

Edit

desired behaviour. from the start

  • i would like the view keyboard to be collapsed (working fine)
  • table view showing the messages (working fine)
  • and ta text field at the bottom of the screen (working fine)

when Text field is tapped

  • keyboard pops up (working)
  • text field moves to top of keyboard (working)
  • table view displays in remaining space above text field (not working, it is above textfield but half is spilling of the screen to the top)

other bug

  • the screen is going black when show/hide the autocomplete words on top of the keyboard
Lonergan6275
  • 1,938
  • 6
  • 32
  • 63

2 Answers2

0

try this for keyboard.

-viewDidLoad()

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)

and

func dismissKeyboard(){
    view.endEditing(true)
}
  • Again the problem is not in displaying or dismissing the keyboard but the displaying of the other elements when the keyboard appears. – Lonergan6275 Mar 16 '16 at 16:36
0

I found the solution and it worked for me.

Please check in your project. (Eendje's solution)

Resize the screen when keyboard appears

You can create an outlet of the bottom auto layout constraint of your table view.

Then simply use this code:

func keyboardWillShow(sender: NSNotification) {
    let info = sender.userInfo!
    var keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
    bottomConstraint.constant = keyboardSize - bottomLayoutGuide.length

    let duration: NSTimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue

    UIView.animateWithDuration(duration) { self.view.layoutIfNeeded() }
}

func keyboardWillHide(sender: NSNotification) {
    let info = sender.userInfo!
    let duration: NSTimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    bottomConstraint.constant = 0

    UIView.animateWithDuration(duration) { self.view.layoutIfNeeded() }
}

If you have trouble creating the bottom constraint:

In storyboard

Select your search bar. At the corner in the lower right you'll see 3 icons. Click the middle one looking like |-[]-|. At the top of that popup, there are 4 boxes. Enter 0 at the one for the bottom. Constraint created! Now you can drag it to your view controller and add it as an outlet.

Another solution is to set the tableView.contentInset.bottom. But I haven't done that before. If you prefer that, I can try to explain it.

Using inset:

func keyboardWillShow(sender: NSNotification) {
    let info = sender.userInfo!
    var keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height

    tableView.contentInset.bottom = keyboardSize
}

func keyboardWillHide(sender: NSNotification) {
    tableView.contentInset.bottom = 0
}

You can try this code for setting the inset. I haven't tried it myself yet, but it should be something like that.

I used constraint solution to fix my problem.

Community
  • 1
  • 1
John
  • 386
  • 1
  • 7
  • 22