How can I create a textfield like iMessages in Swift? I want it to start from the bottom then rise on top of the keyboard when the keyboard appears. Just as iMessages do.
3 Answers
You can implement that by flowing the steps:
1) Insert UIScrollView as subview and set it's constraints to the superview
2) Insert a UITextField as a subview to the scroll view and attach it's bottom constraint to the scroll view
3) Implement the UIKeyboardWillHideNotification and the UIKeyboardWillShowNotification
4) Animate the bottom constraint that holds the text field to the scroll view
5) You can actually achieve the nice UX that iMessage has (interactive keyboard dismiss when you swipe down) just by setting the UIScrollView
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive
PS: If you are going to build a chat app you might want to actually use UICollectionView or give a look at this awesome framework https://github.com/jessesquires/JSQMessagesViewController

- 219
- 1
- 9
This is an issue of keyboard management and setting auto layout correctly.
One option for dealing with the keyboard is something like IQ Keyboard Manager

- 69
- 7
If you want to create it manually, you can put a UITextField
into your storyboard at the bottom of the view, and then give a constraint value of 0 for bottom, trailing and leading edge (and of course a constraint for height). Then create a IBOutlet for the bottom constraint and animate changing the value of that outlet every time the keyboard shows or hides (refer to this link)
I've tried that solution in my personal projects and it works just fine :)