Reputation: 2226
Im building a sample where i can post comments where i am using tableView to add a cell each time i post. its mostly handled but I'm suffering of one issue when i want to post a comment, the tableview bottom is the same as keyboard height but empty and if place it zero the view will move down then pushed top, and thats because I'm moving the whole view when i click on textview to write text.
This is a preview of the empty space under the tableView (keyboard height as inset) : Preview of image on Google Drive
Another image to see if i made the bottom (content inset) = 0 when keyboard is opening : Preview of second image on Google Drive
func keyboardWillShow(sender:NSNotification){
let userInfo: [NSObject : AnyObject] = sender.userInfo!
keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
var contentInsets = UIEdgeInsets()
if wasEmoj == true {
if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) {
contentInsets = UIEdgeInsetsMake(257.0, 0.0, (self.keyboardSize.height), 0.0);
} else {
contentInsets = UIEdgeInsetsMake(257.0, 0.0, (self.keyboardSize.width), 0.0);
}
self.mytableView.contentInset = contentInsets;
self.mytableView.scrollIndicatorInsets = contentInsets
}else {
if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) {
contentInsets = UIEdgeInsetsMake(215.0, 0.0,(self.keyboardSize.height), 0.0);
} else {
contentInsets = UIEdgeInsetsMake(215.0, 0.0, (self.keyboardSize.width), 0.0);
}
self.mytableView.contentInset = contentInsets;
self.mytableView.scrollIndicatorInsets = contentInsets
}
}
func textViewDidBeginEditing(textView: UITextView) {
self.animateTextField(commentText, up: true, movementDistance: -215, duration: 0.3)
}
///- animate the view up or down to adapt with keyboard
func animateTextField (textField:UITextView,up:Bool,movementDistance:Int,duration:NSTimeInterval){
let movement : Int = (up ? movementDistance : -movementDistance);
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
if up == true {
UIView.setAnimationDuration(0.38)
}else {
UIView.setAnimationDuration(0.3)
}
self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
UIView.commitAnimations()
}
Upvotes: 1
Views: 1107
Reputation: 2098
It's better to change the frame of the tableview in this case.
You can keep the instance of the constraint between the commentView's bottom and the bottom layout guide.
@IBOutlet weak var commentViewBottom: NSLayoutConstraint!
Then change the constant of the constraint when needed.
func keyboardWillShow(sender:NSNotification){
if let dic = sender.userInfo {
if let keyboardFrame = dic[UIKeyboardFrameEndUserInfoKey]?.CGRectValue {
if let duration = dic[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue {
commentViewBottom.constant = -keyboardFrame.height
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .CurveLinear, animations: { () -> Void in
self.view.layoutIfNeeded()
}, completion: nil)
}
}
}
}
func keyboardWillHide(sender: NSNotification) {
if let dic = sender.userInfo, let duration = dic[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue {
commentViewBottom.constant = 0
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .CurveLinear, animations: { () -> Void in
self.view.layoutIfNeeded()
}, completion: nil)
}
}
Upvotes: 1