Reputation: 2038
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
when Text field is tapped
other bug
Upvotes: 0
Views: 2000
Reputation: 386
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.
Upvotes: 0
Reputation: 1
try this for keyboard.
-viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
and
func dismissKeyboard(){
view.endEditing(true)
}
Upvotes: 0