【问题标题】:UITextView inside UITableViewCell Keyboard IssueUITableViewCell 中的 UITextView 键盘问题
【发布时间】:2020-03-19 15:04:09
【问题描述】:

作为 IOS 开发的新手,我这几天一直在为键盘移动的问题而苦苦挣扎。我在 UIViewController 内嵌入了具有固定高度约束的 UITableView 的 UISrollView。我还创建了自定义单元格,其中包含不可滚动的 UITextView。问题是当我在 TextView 中输入时键盘不会向下移动。

我遵循了这里的指导:Embedding UITextView inside UITableViewCell,但是只有 UITableView 的示例,而不是 UIScrollView -> UITableView

希望你能理解我的问题。提前致谢

下面附上了一些额外的图片/GIF:

My UIView hierarchy structure

Expected Behaviour of keyboard

Current behaviour of UIKeyboard

查看控制器代码:

import UIKit

class ViewController: UIViewController {

    //MARK: - Constraints
    @IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!

    //MARK: - Outlets
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var tableView: UITableView!

    override func viewWillLayoutSubviews() {
        super.updateViewConstraints()
        self.tableViewHeightConstraint.constant = self.tableView.contentSize.height
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupToHideKeyboardOnTapOnView()
        registerCell()
    }
}

//MARK: - Extensions

extension ViewController {

    func setupUI() {
        tableView.isScrollEnabled = false
    }

    func registerCell() {
        tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: TableViewCell.reuseId)
    }

    func setupToHideKeyboardOnTapOnView()
    {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: #selector(dismissKeyboard))

        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard()
    {
        view.endEditing(true)
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        self.viewWillLayoutSubviews()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: TableViewCell.reuseId, for: indexPath) as! TableViewCell

        cell.textChanged {[weak tableView] (_) in
            DispatchQueue.main.async {
                tableView?.beginUpdates()
                tableView?.endUpdates()
                self.viewWillLayoutSubviews()
            }            
        }
        return cell
    }
}

自定义单元格代码:

import UIKit

class TableViewCell: UITableViewCell, UITextViewDelegate {

    //MARK: - Outlets
    @IBOutlet weak var textView: UITextView!
    var textChanged: ((String) -> Void)?

    static let reuseId = "TableViewCell"

    override func awakeFromNib() {
        super.awakeFromNib()
        textView.isScrollEnabled = false
        textView.delegate = self
    }

    func textChanged(action: @escaping (String) -> Void) {
        self.textChanged = action
    }

    func textViewDidChange(_ textView: UITextView) {
        textChanged?(textView.text)
    }    
}

【问题讨论】:

  • 问题不在于键盘没有向下移动,而是滚动视图没有向上移动。

标签: ios swift uitableview textview keyboard


【解决方案1】:

Chris 是对的,您当前遇到的功能是因为您在滚动视图的中间而不是在滚动视图的底部添加新文本。

如果您想确保您正在编辑的内容始终位于键盘上方,那么您必须计算内容的结束位置和键盘的开始位置,并相应地移动您的视图。

这里有一些额外的info 可能有助于解决键盘问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多