Uitableview and Uiview with Keyboardwillshow

UITableView and UIView with keyboardWillShow

One word: constraints.

Have a read of my article here:
Height of iOS onscreen keyboard

It basically has a constraint at the bottom of the screen, and whenever the user opens the onscreen keyboard, it changes the height of this constaint.

Sample Image

Hope this helps.

Sliding a tableview when keyboard shows up

This is what I tried and it worked perfectly fine as expected.

Can you check where you added your Notification center?

-- Animating contentInsets

override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardWillShow(notification: NSNotification) {
if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
UIView.animate(withDuration: 0.2, animations: {
self.tbl.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
})
}
}

func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
self.tbl.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
})
}

-- Animating autolayout constraint

Make sure you do not have both Bottom layout and height constraints set for UITableView in Storyboard.

NOTE

But there's an even more easy way to do this, just by using a UITableViewController. It will take care of this feature by default. This is, of course, if you have no problem in using it.

EDIT

UITextView will act a little different from UITextField. Set delegate for UITextView. And do all table view alignments in textViewShouldBeginEditing.

extension ViewController: UITextViewDelegate {
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
let pointInTable:CGPoint = textView.superview!.convert(textView.frame.origin, to: tbl)
tbl.contentOffset = CGPoint(x: tbl.contentOffset.x, y: pointInTable.y)
return true
}
}

iOS 8 UIView not moving up when keyboard appears

Your code seems to be correct but i will prefer using UIKeyboardDidChangeFrameNotification or UIKeyboardWillChangeFrameNotification because these will tell you the change in keyboard frame when predictive text bar gets up or down when keyboard is in view.

In your ViewDidLoad add this

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];

and then paste this method in your ViewController

-(void)keyboardFrameDidChange:(NSNotification*)notification{
NSDictionary* info = [notification userInfo];

CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[yourView setFrame:CGRectMake(0, kKeyBoardFrame.origin.y-yourView.frame.size.height, 320, yourView.frame.size.height)];
}

This will handle all your keyboard cases like when its up or down or change in its frame with predictive text bar

and also remove observer when you are leaving your view

My table view automatically resize when keyboard appear

Take an outlet of bottom constraint of tableview and programatically change it like this

tablebottomConstraint.constant =  keyboardEndFrame.size.height+self.messageView.frame.size.height


Related Topics



Leave a reply



Submit