首页 文章

嵌套集合视图单元格未在方向更改时调整大小

提问于
浏览
1

Outer Collection View (在ViewController的文件中)

  • 占据屏幕的整个宽度和高度

  • Cell还占据了屏幕的整个宽度和高度

  • 集合视图布局是水平的 . 能够滑动到下一个单元格 .

  • 单元格背景为绿色

Nested Collection View (在CollectionViewCell的文件中)

  • 占据屏幕的整个宽度和高度

  • Cell占据屏幕的整个宽度 . 身高是100,

  • 细胞紫色的背景 .

Problem

  • 我首先在Xcode的模拟器上以纵向方式运行应用程序 .

  • 当我将方向更改为横向并滑动所有屏幕时,

  • 一个屏幕将有紫色单元格,不占用整个屏幕 . 此问题仅发生在横向中 .

The problem: always one screen that has purple cells like this

Screen Shot

How all cells are suppose to look on orientation landscape

Screen Shot

  • 如果未出现问题,请将方向更改回纵向,然后将其更改为横向 . 然后再次扫描所有单元格以找到问题 .

App Delagate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow()
        window?.makeKeyAndVisible()

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal

        window?.rootViewController = ViewController(collectionViewLayout: layout)

        return true

    }

}

View Controller

import UIKit

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cellid")
        collectionView?.isPagingEnabled = true
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) as! CollectionViewCell
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        collectionViewLayout.invalidateLayout()
    }

}

Collection View Cell (嵌套集合视图)

import UIKit

class CollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .green
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .purple

        addSubview(collectionView)

        collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true

        collectionView.register(InnerCell.self, forCellWithReuseIdentifier: "cellid")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) as! InnerCell
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: frame.width, height: 100)
    }

}

Inner Cell (嵌套集合视图的单元格)

import UIKit

class InnerCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .purple
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

我在github上有这个项目https://github.com/vk99x/Nested-Collection-View

1 回答

  • 1
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        collectionViewLayout.invalidateLayout()
        // Also try reloading your collection view to calculate the new constraints
        DispatchQueue.main.async{
            collectionView.reloadData()
        }
    }
    

    嘿检查这个,让我们看看它是否有帮助

相关问题