[英]UITableView auto scrolling issue on keyboard appearance
我正在使用带有static cells
(1 个单元格)的普通UITableViewController
。 有几个UITextfields
在里面,在StackView
因为它是一个 UITableView,滚动是自动处理的,但问题是当点击 UITextfield 时它会滚动到随机位置。
第一个截图是没有键盘的页面。 第二个屏幕截图是单击名字文本字段时(正常行为) 第三个屏幕截图是单击姓氏文本字段时(??!! 行为) 第四个屏幕截图是单击电子邮件文本字段时(再次 ???!! 行为)
我没有使用任何外部键盘处理库
我没有使用任何代码来添加插图或处理滚动或处理键盘。
文本字段按正确顺序放置(TxtFname,TxtLname,TxtFatherName....)
UITextfields 放置在堆栈视图中。 有人可以告诉我似乎是什么问题吗?
您可以使用第三方通过滚动 UIView 或 UITableview 来处理键盘。
pod 'IQKeyboardManager' 这是可以帮助您管理滚动的第三方。 如果您不想滚动标题,请将其置于与表格视图不同的视图中。 并确保您正确使用了自动布局。
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if tblProfile.isDecelerating {
view.endEditing(true)
}
}
当您手动滚动 tableview 而不使用键盘进入下一个文本字段时,使用此代码将调用此方法并将隐藏键盘。 点击文本字段后,键盘会再次出现。
您可以查看我以前的解决方案。 它完全处理所描述的问题。
我已经为这个问题做了一个创可贴修复,结合了这里发布的不同答案和一些额外的答案。
override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(animated)
}
评论viewWillAppear(animated)
会禁用 UITableViewController 的自动滚动。
现在在 ViewDidLoad() 中添加了 2 个通知观察者
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
以及它们的功能
@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)
}
这将确保即使键盘可见,也有额外的空间供用户滚动到页面底部。
现在我使用IQKeyboardManager
来实现 UP n DOWN 箭头的额外功能,以移动到下一个UITextField
和 DONE 按钮以关闭键盘。
嗨面临类似的问题使用
带有自定义单元格的 UITableview(具有 xib 和多个文本字段)具有下拉列表() 和IQKeyboardManager (第三方使用)
轻松修复
在 viewDidload 里面你刚刚放
yourTextField.inputView.inputView = UIView()
yourTextField.inputView.inputAccessoryView = UIView()
尝试一下。 我希望这会帮助你: https ://medium.com/@hassanahmedkhan/scrolling-the-hell-out-of-stackview-33d239f9f38e 或者你可以使用这样的 uitable 视图: https ://medium.com/@how_noobs_think /uitableviewcontroller-will-automatically-adjust-content-position-relative-to-keyboard-ios-swift-8810a375ffb2
或简单: 使用 Swift 使用键盘移动视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.