Thuc Pham
Thuc Pham

Reputation: 153

UITextFieldViewModeWhileEditing change my TextField frame

I got an weird issue. Please help me in this case.I had a bottom View which contain a TextField like this image

enter image description here When I type text in textfield, I want my bottom view move above the keyboard and be like this:

enter image description here

However, after typing one character, my bottom view will move back to this previous position enter image description here

I found out that whenever I set

self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;

my bottom view will move back to its previous position. This is my source code link: https://github.com/duythuc28/ProblemUITextField

Thanks for your help.

Upvotes: 1

Views: 461

Answers (2)

Fullmoon
Fullmoon

Reputation: 21

You are using Autolayout, so you cannot set frame for the view. It should reset the constraint.

Or you can use third-party such as IQKeyboardManager to handle your keyboard event with worries free. IQKeyboardManager can be used in both ObjectiveC and Swift. It can also handle the appearance of the suggestion on keyboard (which I realize in your code that haven't handled yet) Here is the link: https://github.com/hackiftekhar/IQKeyboardManager

Upvotes: 1

Oleshko
Oleshko

Reputation: 3093

First of all, change observers from UIKeyboardDidShowNotification to UIKeyboardWillShowNotification and from UIKeyboardDidHideNotification to UIKeyboardWillHideNotification. Second: don't change view's frame. You have bottom view's constraint to bottom (equal to 0); create outlet for this constraint and in method keyboardWillShow set it equal to keyboard height and in keyboardWillHide to 0.

Upvotes: 1

Related Questions