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

标签 ios swift xcode uicollectionview 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)!
}

最佳答案

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

相关文章:

Objective-C 静音和取消静音的方法

iOS 应用程序在模拟器上工作但不能通过 TestFlight

ios - 如何通过下载数据更新标签

ios - Swift 中的 KVO,observeValueForKeyPath 的有趣问题

ios - 为什么在将 AVAssetReader 与音频队列一起使用时音频出现乱码

ios - 在 iOS Swift 中使用委托(delegate)和协议(protocol)从 dataTaskWithRequest 返回数据

Java:推送通知已发送但未收到

ios - 为 Mac 的 UIKit 构建,但链接框架 'XXX.framework' 是为 macOS 构建的

swift - Swift 中的 Hashable 和 Equatable 有什么用?什么时候使用哪个?

xcode - SKTexture 没有正确改变