简体   繁体   English

键盘外观上的 UITableView 自动滚动问题

[英]UITableView auto scrolling issue on keyboard appearance

I am using a normal UITableViewController with static cells (1 cell).我正在使用带有static cells (1 个单元格)的普通UITableViewController 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.有几个UITextfields在里面,在StackView因为它是一个 UITableView,滚动是自动处理的,但问题是当点击 UITextfield 时它会滚动到随机位置。

这是第一个屏幕这是第二个屏幕这是第三屏这是第四屏

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....)文本字段按正确顺序放置(TxtFname,TxtLname,TxtFatherName....)

The UITextfields are placed inside a stackview. UITextfields 放置在堆栈视图中。 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.您可以使用第三方通过滚动 UIView 或 UITableview 来处理键盘。
pod 'IQKeyboardManager' This the third party which can help you manage the scrolling. pod 'IQKeyboardManager' 这是可以帮助您管理滚动的第三方。 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.当您手动滚动 tableview 而不使用键盘进入下一个文本字段时,使用此代码将调用此方法并将隐藏键盘。 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.评论viewWillAppear(animated)会禁用 UITableViewController 的自动滚动。

Now 2 notification observers are added in ViewDidLoad()现在在 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)

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.现在我使用IQKeyboardManager来实现 UP n DOWN 箭头的额外功能,以移动到下一个UITextField和 DONE 按钮以关闭键盘。

Hi face similar issue using嗨面临类似的问题使用

UITableview with custom cell (having xib and a number of textfield) Having dropdown () and IQKeyboardManager (third party used )带有自定义单元格的 UITableview(具有 xib 和多个文本字段)具有下拉列表() 和IQKeyboardManager (第三方使用)

Easily fix it轻松修复

Inside viewDidload you just put在 viewDidload 里面你刚刚放

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我希望这会帮助你: 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

or simple: Move view with keyboard using Swift或简单: 使用 Swift 使用键盘移动视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM