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