首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌套集合视图单元格不对方向更改进行调整

嵌套集合视图单元格不对方向更改进行调整
EN

Stack Overflow用户
提问于 2018-01-14 16:15:56
回答 1查看 1.2K关注 0票数 3

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

  • 占用屏幕的全部宽度和高度
  • 单元格还占用屏幕的整个宽度和高度。
  • 集合视图布局是水平的。能够滑动到下一个牢房。
  • 细胞背景为绿色

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

  • 占用屏幕的整个宽度和高度。
  • 单元格占用屏幕的整个宽度。高度是100,
  • 细胞紫色的背景。

问题

  1. 我首先在Xcode的模拟器上以纵向方向运行这个应用程序。
  2. 当我把方向改变为风景和滑动所有屏幕时,
  3. 一个屏幕的紫色细胞不会占据整个屏幕。这个问题只发生在景观中。

问题:总是有一个像这样的紫色细胞的屏幕

所有的细胞应该如何看待方向景观

  1. 如果问题没有发生,请将方向更改为肖像,然后将其改为景观。然后再扫遍所有的细胞找出问题。

应用程序分层

代码语言:javascript
复制
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
        
    }
    
}

视图控制器

代码语言:javascript
复制
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()
    }
    
}

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

代码语言:javascript
复制
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)
    }
    
}

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

代码语言:javascript
复制
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的项目

EN

回答 1

Stack Overflow用户

发布于 2018-12-10 09:04:58

代码语言:javascript
复制
 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()
    }
}

嘿,看看这个,看看是否有用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48251561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档