OGeek|极客世界-中国程序员成长平台

标题: ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:00
标题: ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout

我有一个 Collection View ,它显示具有动态内容大小的文章的详细 View 。
我在 Collection View 中有多个可以水平滚动的项目。

当水平流布局处于事件状态时,动态单元格无法垂直滚动,内容隐藏在 View 下。

如果我禁用水平流布局,我可以垂直滚动内容。但是,所有其他项目都堆叠在每个单元格的底部。

我基本上是在寻找类似布局的新闻门户,用户可以在其中垂直滚动浏览文章的内容,也可以水平滚动浏览文章列表。

我以这种方式设置了我的收藏 View 。

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
}()

Collection View 单元格有一个动态高度,需要垂直滚动,我在这里设置了。

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)!
}



Best Answer-推荐答案


你试过了吗?设置collectionView

    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: screenWidth/2,height: 200)
    layout.scrollDirection = .horizontal
    layout.minimumInteritemSpacing = 1
    layout.minimumLineSpacing = 1
    collectionView.setCollectionViewLayout(layout, animated: true)

并在点击按钮时将 Collection View 设置为水平或垂直滚动​​,在 viewDidLoad() 上方全局声明布局。

关于ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49751582/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4