I am using a normal UITableViewController
with static cells
(1 cell). Having several UITextfields
in it,inside StackView
Since it is a UITableView, the scrolling is handled automatically but the problem is that it scrolls to random positions when a UITextfield is clicked.
The first screenshot is the page without a keyboard. The second screenshot is when First Name textfield is clicked (normal behavior) The third screenshot is when Last Name textfield is clicked (??!! behavior) The fourth screenshot is when Email textfield is clicked (again ???!! behavior)
I am not using any external keyboard handling libraries
I am not using any code to add insets or handle scrolling or handle the keyboard.
The Textfields are placed in correct order(TxtFname,TxtLname,TxtFatherName....)
The UITextfields are placed inside a stackview. Can someone tell me what seems to be the problem?
You can use the third party to handle the keyboard with scrolling of a UIView or a UITableview.
pod 'IQKeyboardManager' This the third party which can help you manage the scrolling. If you don't want to scroll the header make it in different view apart from table view. And make sure you have use the auto layouts properly.
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if tblProfile.isDecelerating {
view.endEditing(true)
}
}
Use this code when you are scrolling the tableview manually without using the keyboard to go on next text field this method will be called and will hide the keyboard. Once you tap on textfield the keyboard appears again.
You can go over my previous solution. It exactly handle the issue described.
moving-keyboard-when-editing-multiple-textfields-with-constraints-swift
I have made a band-aid fix for this problem , combing different answers posted here and little bit extra.
override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(animated)
}
Commenting the viewWillAppear(animated)
disables the automatic scrolling of UITableViewController.
Now 2 notification observers are added in ViewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
and their function as
@objc func keyboardWillShow(notification: NSNotification) {
if !isKeyboardShowing {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber
adjustTableViewInsets(keyboardHeight: keyboardHeight, duration: duration, curve: curve)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber
adjustTableViewInsets(keyboardHeight: 0, duration: duration, curve: curve)
}
This will make sure that there is extra space for the user to scroll to the bottom of the page even if keyboard is visible.
Now i used IQKeyboardManager
for the extra functionality of UP n DOWN arrows to move to next UITextField
and DONE button to dismiss keyboard.
Hi face similar issue using
UITableview with custom cell (having xib and a number of textfield) Having dropdown () and IQKeyboardManager (third party used )
Easily fix it
Inside viewDidload you just put
yourTextField.inputView.inputView = UIView()
yourTextField.inputView.inputAccessoryView = UIView()
try it. I hope that will help you: https://medium.com/@hassanahmedkhan/scrolling-the-hell-out-of-stackview-33d239f9f38e or you could use uitable view like that: https://medium.com/@how_noobs_think/uitableviewcontroller-will-automatically-adjust-content-position-relative-to-keyboard-ios-swift-8810a375ffb2
or simple: Move view with keyboard using Swift
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.