Cell 展開收合效果
tableView
ViewController 的 viewDidLoad 加上
tableView.estimatedRowHeight = 60 tableView.rowHeight = UITableView.automaticDimension
collectionView
ViewController 的 viewDidLoad 加上
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout flowLayout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.width - 20, height: 200)
ExpandingCollectionViewCell 加上
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { setNeedsLayout() layoutIfNeeded() let size = contentView.systemLayoutSizeFitting(layoutAttributes.size) var frame = layoutAttributes.frame frame.size.height = ceil(size.height) layoutAttributes.frame = frame return layoutAttributes }
storyboard
tableViewCell 下的 contenView 加入 stackView,再放進 titileLabel 和 contentStackView
collectionView 本身沒有 contentView,所以自己加一個 UIView 充當 contentView,再重複上述步驟
contentStackView 再放進 nameLabel 和 ageLabel
hugging priority 要設定
最外層的 stackView 要設定 bottom constraint,並且權重設定小於1000
藉由操作 contentStackView.isHidden 達到展開收合效果
Last updated
Was this helpful?