2

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

当水平流布局处于活动状态时,动态单元格无法垂直滚动并且内容隐藏在视图下。

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

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

我已经以这种方式设置了我的收藏视图。

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

集合视图有多个项目,需要水平滚动。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return (blogContainer?.blogs.count)!
}
4

1 回答 1

0

你试过了吗?设置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() 上方全局声明布局。

于 2018-04-10T11:28:06.780 回答