UICollectionView 从纵向到横向
UICollectionView from portrait to landscape
我在我的项目中使用了 UICollectionView。我开始以编程方式完成所有工作(在 swift 中)。
这是我的代码,用于启动 collectionView 并将其添加到屏幕:
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: COLLECTION_VIEW_DISTANCE_TOP, left: COLLECTION_VIEW_DISTANCE_LEFT, bottom: COLLECTION_VIEW_DISTANCE_BOTTOM, right: COLLECTION_VIEW_DISTANCE_RIGHT)
layout.itemSize = CGSize(width: 90, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
}
全大写字母的变量是我之前创建的常量
一切正常,除了当我从纵向切换到横向时,每行中的项目数没有改变,但当切换到横向时,该行中的项目应该比以前多。
我试过覆盖这个函数:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
collectionView.frame = self.view.frame
}
它起作用了,但是当旋转 iPhone 时,它需要大约半秒来填充行(它以一种丑陋的方式缓慢地完成工作)。
我该如何解决?
Apple 弃用了 iOS 8 SDK 中的 willRotateMethod 方法。所以你在 didRotateMethod 方法中编写的代码就是你将在 viewDidLayoutSubviews
中编写的代码,这样你的问题就会得到解决。
我在我的项目中使用了 UICollectionView。我开始以编程方式完成所有工作(在 swift 中)。 这是我的代码,用于启动 collectionView 并将其添加到屏幕:
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: COLLECTION_VIEW_DISTANCE_TOP, left: COLLECTION_VIEW_DISTANCE_LEFT, bottom: COLLECTION_VIEW_DISTANCE_BOTTOM, right: COLLECTION_VIEW_DISTANCE_RIGHT)
layout.itemSize = CGSize(width: 90, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
}
全大写字母的变量是我之前创建的常量
一切正常,除了当我从纵向切换到横向时,每行中的项目数没有改变,但当切换到横向时,该行中的项目应该比以前多。
我试过覆盖这个函数:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
collectionView.frame = self.view.frame
}
它起作用了,但是当旋转 iPhone 时,它需要大约半秒来填充行(它以一种丑陋的方式缓慢地完成工作)。
我该如何解决?
Apple 弃用了 iOS 8 SDK 中的 willRotateMethod 方法。所以你在 didRotateMethod 方法中编写的代码就是你将在 viewDidLayoutSubviews
中编写的代码,这样你的问题就会得到解决。