显示键盘时如何使表格自动向上滚动?

时间:2018-10-11 12:28:42

标签: swift uitableview

第一个选项(有效) 如果使用UITableViewController类,则向上滚动会自动进行。

第二个选项(不起作用) 但是,如果使用UIViewController + UITableView,则表不会自动向上滚动。

请告诉我第二种选择该怎么办?

调整tableView大小

func keyboardWillShow(_ notification: Notification) {
    //get the end position keyboard frame
    guard let keyInfo = notification.userInfo as NSDictionary? else {
        return
    }
    var keyboardFrame: CGRect = keyInfo.object(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! CGRect
    //CGRect keyboardFrame = [[keyInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    //convert it to the same view coords as the tableView it might be occluding
    keyboardFrame = self.tableView.convert(keyboardFrame, from: nil)
    //calculate if the rects intersect
    let intersect: CGRect = keyboardFrame.intersection(self.tableView.bounds)
    if (intersect != CGRect.null) {
        //yes they do - adjust the insets on tableview to handle it
        //first get the duration of the keyboard appearance animation
        let duration: TimeInterval = keyInfo.object(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! Double
        // adjust the animation curve - untested
        let curve: Int = (notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! Int) << 16
        //change the table insets to match - animated to the same duration of the keyboard appearance
        UIView.animate(withDuration: duration, delay: 0.2, options: UIView.AnimationOptions(rawValue: UInt(curve)), animations: {
            let height = intersect.size.height
            self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: height, right: 0)
            self.tableView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: height, right: 0)
        }, completion: nil)
    }
}
func keyboardWillHide(_ notification: Notification) {
    let keyInfo: NSDictionary = notification.userInfo! as NSDictionary
    let duration: TimeInterval = keyInfo.object(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! Double
    let curve: Int = (notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! Int) << 16
    //change the table insets to match - animated to the same duration of the keyboard appearance
    UIView.animate(withDuration: duration, delay: 0.2, options: UIView.AnimationOptions(rawValue: UInt(curve)), animations: {
        self.tableView.contentInset = UIEdgeInsets.zero
        self.tableView.scrollIndicatorInsets = UIEdgeInsets.zero
    }, completion: nil)
}

如何滚动到所需的单元格?如何找到光标所在的单元格?

为单元格添加了其他功能

class CustomTableViewCell: UITableViewCell {
    func scrollToCell() {
        if let tableView = self.getTableView() {
            if let index = tableView.indexPath(for: self) {
                UIView.animate(withDuration: 0.2, animations: {
                    tableView.scrollToRow(at: index, at: .none, animated: false)
                }, completion: nil)
            }
        }
    }
    private func getTableView() -> UITableView? {
        return (self.superview as? UITableView)
    }
}

我在发生事件时调用此函数:

UITextView::textViewDidBeginEditing
UITextField::textFieldDidBeginEditing

我认为有更好的方法

1 个答案:

答案 0 :(得分:0)

您可以观察何时显示键盘,然后将表滚动到顶部,如下所示:

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector:
            #selector(keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
    }

    @objc
    func keyboardWillShow(_ notification: Notification) {
        let indexPath = IndexPath(row: 0, section: 0)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }