0

I am trying to implement a very basic chat feature into my app and I am using constraints to keep everything in the correct place. It is great except for when I need to actually type, and the problem that arises is that the keyboard covers the text field and I not only cannot see the textfield but I cannot dismiss it. Thank you for all help!

In summary,

  • Using a textfield with contraints at bottom of screen
  • keyboard shows up and covers it, and I cannot dismiss the keyboard
Ryan Cocuzzo
  • 2,538
  • 4
  • 28
  • 51
  • Possible duplicate of [How to make a UITextField move up when keyboard is present?](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Akshansh Thakur Jul 04 '16 at 19:03

2 Answers2

1

Just set observers for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification.

Whenever, UIKeyboardWillShowNotification is triggered, move the UITextfield upwards equivalent to the keyboard height. Then, when the UIKeyboardWillHideNotification is triggered, move the keyboard back into place.

7vikram7
  • 2,424
  • 1
  • 20
  • 38
  • how do you get the keyboard height? @7Vikram7 – Ryan Cocuzzo Jul 04 '16 at 19:39
  • http://stackoverflow.com/questions/11989306/get-the-frame-of-the-keyboard-dynamically – 7vikram7 Jul 04 '16 at 19:42
  • http://stackoverflow.com/questions/25874975/cant-get-correct-value-of-keyboard-height-in-ios8 – 7vikram7 Jul 04 '16 at 19:42
  • @Ryan Refer these 2 – 7vikram7 Jul 04 '16 at 19:42
  • both of those are in Objective-C – Ryan Cocuzzo Jul 04 '16 at 19:43
  • NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) //////////// func keyboardWillShow(notification:NSNotification) { let userInfo:NSDictionary = notification.userInfo! let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue let keyboardRectangle = keyboardFrame.CGRectValue() let keyboardHeight = keyboardRectangle.height } – 7vikram7 Jul 04 '16 at 19:45
1

Dismiss keyboard by tapping anywhere

override func viewDidLoad() 
{
    super.viewDidLoad() 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)}

func dismissKeyboard() 
{
    view.endEditing(true)
}
Mukul_3062
  • 86
  • 8
  • this does not solve the issue of not seeing the text but is a very helpful snippet of code because I did not know how to do that – Ryan Cocuzzo Jul 05 '16 at 06:24
  • now ur keyboard is going down or not?? by tapping outside?? – Mukul_3062 Jul 05 '16 at 08:37
  • you have to use the constraints very well to show textfield. Use priorities in the constraint. I think you can do that easily or make the page scrollable. – Mukul_3062 Jul 05 '16 at 08:40