【问题标题】:Nested collection view cells not resizing on orientation change嵌套的集合视图单元格未在方向更改时调整大小
【发布时间】:2018-01-14 16:15:56
【问题描述】:

外部集合视图 (在ViewController的文件中)

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

  • 单元格也占据了屏幕的整个宽度和高度

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

  • 单元格背景为绿色

嵌套集合视图 (在CollectionViewCell的文件中)

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

  • 单元格占据屏幕的整个宽度。身高100,

  • 单元格背景为紫色。

问题

  1. 我首先在 Xcode 的模拟器上纵向运行应用程序。

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

  3. 一个屏幕会有不占整个屏幕的紫色单元格。这个问题只发生在横向。

问题:总是有这样的紫色单元格的屏幕

假设所有单元格在方向景观上的外观

  1. 如果没有出现问题,请将方向更改回纵向,然后将其更改为横向。然后再次滑动所有单元格以查找问题。

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

视图控制器

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

集合视图单元格 (具有嵌套集合视图)

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

内部单元格 (嵌套集合视图的单元格)

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

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案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()
        }
    }
    

    嘿,检查一下,看看它是否有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2018-03-24
      • 1970-01-01
      • 2021-11-02
      • 2023-03-16
      相关资源
      最近更新 更多