【发布时间】:2018-09-19 22:51:16
【问题描述】:
我有一个集合视图,它显示具有动态内容大小的文章的详细视图。
我在集合视图中有多个可以水平滚动的项目。
当水平流布局处于活动状态时,动态单元格无法垂直滚动,内容隐藏在视图下。
如果我禁用水平流布局,我可以垂直滚动内容。但是,所有其他项目都堆叠在每个单元格的底部。
我基本上是在寻找类似布局的新闻门户,用户可以在其中垂直滚动浏览文章的内容,也可以水平滚动浏览文章列表。
我以这种方式设置了我的收藏视图。
lazy var blogDetailCollectionView: UICollectionView =
{
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = 0
//layout.itemSize = UICollectionViewFlowLayoutAutomaticSize
let blogDetailCollection = UICollectionView(frame: .zero, collectionViewLayout: layout)
blogDetailCollection.delegate = self
blogDetailCollection.dataSource = self
blogDetailCollection.backgroundColor = .white
blogDetailCollection.isPagingEnabled = true
blogDetailCollection.translatesAutoresizingMaskIntoConstraints = false
return blogDetailCollection
}()
集合视图单元格有一个动态高度,需要垂直滚动,我在这里设置了。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
guard let blogDescription = blogContainer?.blogs[indexPath.item].blogDescription else {return CGSize(width: frame.width, height: frame.height)}
let widthOfTextView = frame.width
let size = CGSize(width: widthOfTextView, height: 1000)
let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: UIFont.labelFontSize)]
let estimatedFrame = NSString(string: blogDescription).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
let blogImageHeight = frame.width * (2 / 3)
return CGSize(width: widthOfTextView, height: estimatedFrame.height + blogImageHeight)
}
collection view 有多个item,需要横向滚动。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return (blogContainer?.blogs.count)!
}
【问题讨论】:
标签: ios swift xcode uicollectionview uicollectionviewflowlayout