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.
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
How to Dismiss Viewcontroller in Swift
Select Multiple Images (Uiimagepickercontroller or Photos.App Share Ui)
iOS App Error - Can't Add Self as Subview
Loading an Image into Uiimage Asynchronously
Counting the Number of Lines in a Uitextview, Lines Wrapped by Frame Size
How to Access Own Window Within Swiftui View
Swift Apply .Uppercasestring to Only the First Letter of a String
iOS 13: Swift - 'Set Application Root View Controller Programmatically' Does Not Work
No Umbrella Header Found for Target, Module Map Will Not Be Generated
How to Play a Sound Using Swift
How to Change Locale Programmatically with Swift
Nspersistentcontainer Concurrency for Saving to Core Data
Get Size of a View in React Native
How to Rotate Text for Uibutton and Uilabel in Swift
Get Progress from Datataskwithurl in Swift
Arkit - Apply Cifilter to a Specific Vertices of Arfaceanchor