Swift - 具有动态高度的 UICollectionViewCells
Swift - UICollectionViewCells with dynamic height
我正在开发一个聊天应用程序,但我不知道如何根据 UITextView
的文本大小使 UICollectionViewCell
的高度动态变化。
这是我一直在测试的内容;
class ViewController: UIViewController {
@IBOutlet var cv: UICollectionView!
let arr = ["hi hello", "func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell", "func collectionView", "// Do any additional setup after loading the view.", "extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let flowLayout = cv.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
flowLayout.itemSize = UICollectionViewFlowLayout.automaticSize
}
// setup cv constraints here
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mm", for: indexPath) as! MessageCollectionViewCell
cell.setup(text: arr[indexPath.row])
return cell
}
}
这是我手机的密码;
class MessageCollectionViewCell: UICollectionViewCell {
@IBOutlet var tv: UITextView!
func setup(text: String) {
tv.text = text
tv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tv.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0),
tv.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 8),
tv.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -8),
tv.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0)
])
}
}
这就是最终结果;
知道我错过了什么吗?
我终于修好了!我应该做的就是为单元格本身添加一个宽度约束,这样它就不会在水平方向上变得比它需要的更大!
NSLayoutConstraint.activate([
self.contentView.widthAnchor.constraint(equalToConstant: maxDeviceWidth)
])
我正在开发一个聊天应用程序,但我不知道如何根据 UITextView
的文本大小使 UICollectionViewCell
的高度动态变化。
这是我一直在测试的内容;
class ViewController: UIViewController {
@IBOutlet var cv: UICollectionView!
let arr = ["hi hello", "func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell", "func collectionView", "// Do any additional setup after loading the view.", "extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let flowLayout = cv.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
flowLayout.itemSize = UICollectionViewFlowLayout.automaticSize
}
// setup cv constraints here
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mm", for: indexPath) as! MessageCollectionViewCell
cell.setup(text: arr[indexPath.row])
return cell
}
}
这是我手机的密码;
class MessageCollectionViewCell: UICollectionViewCell {
@IBOutlet var tv: UITextView!
func setup(text: String) {
tv.text = text
tv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tv.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0),
tv.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 8),
tv.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -8),
tv.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0)
])
}
}
这就是最终结果;
知道我错过了什么吗?
我终于修好了!我应该做的就是为单元格本身添加一个宽度约束,这样它就不会在水平方向上变得比它需要的更大!
NSLayoutConstraint.activate([
self.contentView.widthAnchor.constraint(equalToConstant: maxDeviceWidth)
])