gpt4 book ai didi

ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:35 25 4
gpt4 key购买 nike

我有一个 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)!
}

最佳答案

你试过吗.设置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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com