ios - TextView 无法正确调整大小

标签 ios swift uitableview uitextview

我有一个表格 View 单元格,里面有一个 TextView :

class DetailsCell: UITableViewCell, UITextViewDelegate {


    let detailsTextView: UITextView = {
        let tv = UITextView()
        tv.font = UIFont(name: "AvenirNext-Medium", size: 24)
        tv.isScrollEnabled = false
        tv.textColor = .white
        tv.backgroundColor = .clear
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
    }()


    func textViewDidChange(_ textView: UITextView) {
        let addTodoViewController = AddTodoViewController()
        addTodoViewController.begindEndUpdate()
    }



    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: "DetailsCell")

        addSubview(detailsTextView)
        detailsTextView.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
        detailsTextView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        detailsTextView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
        detailsTextView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true


        backgroundColor = .black

        textLabel?.isHidden = true

        detailsTextView.delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

当我加载应用程序并且 TextView 已经有一个以编程方式分配的文本时,单元格正确获得了正确的高度并且 TextView 正确调整了大小,但是当我尝试更改 TextView 的文本时,单元格和 TextView 不要调整大小。有人能帮我吗?

 override func viewWillAppear(_ animated: Bool) {
    toDoTextField.becomeFirstResponder()
    addTodoTableView.estimatedRowHeight = 60
    addTodoTableView.rowHeight = UITableViewAutomaticDimension
}

Textview with not initial text Image

Textview with initial text Image

最佳答案

UITextView 在内容更改时不会调整自身大小。您必须编写一些代码来调整 textView 的大小,然后相应地调整单元格的高度。也许这可以帮助你resizing textview to its content

或者您可以使用一些第三方库来代替默认的UITextView,例如GrowingTextView

关于ios - TextView 无法正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48289828/

相关文章:

iphone - Cocos2d v2.0 : unable to add CCParticleSystem to scene

ios - 如何在 Firebase 中保存的数据下构建数据

iphone - 更好地使用图像或以编程方式自定义 UINavigationBar?

swift - 快速将 4 个字节的 Float 给出一个意想不到的小结果

ios - 在两个嵌套子类中调用 super.viewDidLoad() 时,我遇到了 Swift 的无限循环

ios - 有没有办法在 MacBook M 上调试 iOS 应用或使用 TestFlight?

ios - 分页时优化数组数据大小

iphone - 插入新行时 UITableViewCell 按钮未获取标记

iphone - UITableViewCell 滑动抽屉

swift - 在 SwiftUI 中隐藏导航栏而不会丢失向后滑动手势