Swift:如何在设备旋转后刷新UICollectionView布局 [英] Swift: How to refresh UICollectionView layout after rotation of the device
本文介绍了Swift:如何在设备旋转后刷新UICollectionView布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用UICollectionView(flowlayout)来构建一个简单的布局。
每个单元格的宽度使用 self.view.frame.width
<设置为屏幕宽度p>但是当我旋转设备时,单元格不会更新。
我找到了一个函数,在方向改变时调用:
覆盖func willRotateToInterfaceOrientation(toInterfaceOrientation) :
UIInterfaceOrientation,持续时间:NSTimeInterval){
//代码
}
但我无法找到更新UICollectionView布局的方法
主要代码在这里:
class ViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var myCollection:UICollectionView!
var numOfItemsInSecOne:Int!
覆盖func viewDidLoad(){
super.viewDidLoad()
numOfItemsInSecOne = 8
//加载视图后,通常从笔尖进行任何其他设置。
}
覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}
覆盖func willRotateToInterfaceOrientation(toInterfaceOrientation:UIInterfaceOrientation,duration:NSTimeInterval){
// print(orientation Changed)
}
func numberOfSectionsInCollectionView(collectionView:UICollectionView) - > Int {
return 1
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection section:Int) - > Int {
return numOfItemsInSecOne
}
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath) - > UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellO,forIndexPath:indexPath)
return cell
}
func collectionView(collectionView:UICollectionView ,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) - > CGSize {
let itemSize = CGSize(width:self.view.frame.width,height:100)
return itemSize
}}
解决方案
添加此功能:
覆盖func viewDidLayoutSubviews(){
super.viewDidLayoutSubviews()
myCollection.collectionViewLayout.invalidateLayout()
}
当您更改方向时,将调用此函数。
I used UICollectionView (flowlayout) to build a simple layout.
the width for each cell is set to the width of screen using self.view.frame.width
but when I rotate the device, the cells don't get updated.
I have found a function, which is called upon orientation change :
override func willRotateToInterfaceOrientation(toInterfaceOrientation:
UIInterfaceOrientation, duration: NSTimeInterval) {
//code
}
but I am unable to find a way to update the UICollectionView layout
The main code is here:
class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{
@IBOutlet weak var myCollection: UICollectionView!
var numOfItemsInSecOne: Int!
override func viewDidLoad() {
super.viewDidLoad()
numOfItemsInSecOne = 8
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
//print("orientation Changed")
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numOfItemsInSecOne
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellO", forIndexPath: indexPath)
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
let itemSize = CGSize(width: self.view.frame.width, height: 100)
return itemSize
}}
解决方案
Add this function:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
myCollection.collectionViewLayout.invalidateLayout()
}
When you change the orientation, this function would be called.
这篇关于Swift:如何在设备旋转后刷新UICollectionView布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文