将可扩展的uitextview单元格添加到uicollectionview

发布于 2025-02-09 17:56:51 字数 1238 浏览 1 评论 0原文

当其中一个单元格是包含uitextview的单元格时,我有收集视图。我想要的是将textView添加到此单元格中,该单元将相应地扩展到输入文本。

我创建了具有文本视图内部文本视图的Uiview:

private var textView: UITextView = {
    let textView = UITextView()
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.isUserInteractionEnabled = true
    textView.isEditable = true
    return textView
  }()

内部我设置了对监督的约束如下:

private func setConstraints() {
  let width: CGFloat = 312
    textView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    textView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    textView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    textView.widthAnchor.constraint(equalToConstant: width).isActive = true
}

然后在单元格I内部设置这样的约束:

func setConstraints() {
textView.topAnchor.equalTo(topAnchor).isActive = true
textView.leftAnchor.equalTo(leftAnchor).isActive = true
textView.rightAnchor.equalTo(rightAnchor).isActive = true
textView.bottomAnchor.equalTo(bottomAnchor).isActive = true
}

我确实在文本视图增长后扩展时确实实现了行为,但是,我没有设置底部约束对于uitextview- uiview,当我在单元格中键入大量文本时,我的收集视图不会增长(滚动)的原因。 如何在内部使用UITEXTVIEW添加单元格,它将扩展其属于的收集视图?

I have collection view when one of it's cell is cell that contain UITextView. What i want is to add textView to this cell that will expand accordingly to entered text.

I created UIView that have text view inside:

private var textView: UITextView = {
    let textView = UITextView()
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.isUserInteractionEnabled = true
    textView.isEditable = true
    return textView
  }()

Inside i set up it's constraints to superview as following:

private func setConstraints() {
  let width: CGFloat = 312
    textView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    textView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    textView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    textView.widthAnchor.constraint(equalToConstant: width).isActive = true
}

Then inside cell i set constraints like that:

func setConstraints() {
textView.topAnchor.equalTo(topAnchor).isActive = true
textView.leftAnchor.equalTo(leftAnchor).isActive = true
textView.rightAnchor.equalTo(rightAnchor).isActive = true
textView.bottomAnchor.equalTo(bottomAnchor).isActive = true
}

I did achieve behaviour when cell is expanding after textView growth, but, i didn't set bottom constraint for UITextView->UIView, that why my collection view doesn't grow (scroll) when i type large bunch of text in my cell.
How to add cell with UITextView inside that will expand CollectionView it belongs to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2025-02-16 17:56:51

我认为,使用手动估算文本视图高度和在这种方法中返回的一种更好的方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  

比使用AutoLayout更加困难,但是提供了更多的控制和自定义可能性。

I think a better way to use manual estimation text view height and return one in

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  

This approach is more difficult than using AutoLayout but gives more control and customization possibilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文