菜鸟教程小白 发表于 2022-12-11 20:00:48

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


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

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

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

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

<p>我以这种方式设置了我的收藏 View 。 </p>

<pre><code>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
}()
</code></pre>

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

<pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize
{
    guard let blogDescription = blogContainer?.blogs.blogDescription else {return CGSize(width: frame.width, height: frame.height)}

    let widthOfTextView = frame.width
    let size = CGSize(width: widthOfTextView, height: 1000)
    let attributes =

    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)
}
</code></pre>

<p>collection view有多个item,需要横向滚动。</p>

<pre><code>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int
{
    return (blogContainer?.blogs.count)!
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你试过了吗?设置collectionView</p>

<pre><code>    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)
</code></pre>

<p>并在点击按钮时将 Collection View 设置为水平或垂直滚动​​,在 viewDidLoad() 上方全局声明布局。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49751582/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49751582/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout