【问题标题】:Dynamic UIViewCell Vertical Cell scrolling with Horizontal UICollectionViewFlowLayout动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout
【发布时间】: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


    【解决方案1】:

    你试过了吗?设置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)
    

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

    【讨论】:

    • 如何设置水平和垂直滚动的collectionview?上面的代码和我做的几乎一样,它阻止了单元格的内容垂直滚动。
    • 如果按下垂直按钮,我假设然后粘贴我作为答案发布的相同代码并将滚动方向更改为垂直
    • 我已经编辑了我的问题并添加了代码,也许你现在会明白这个问题了。
    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 2021-11-13
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2011-01-12
    相关资源
    最近更新 更多