UICollectionView(flowlayout)を使用して簡単なレイアウトを作成しました。 各セルの幅は、self.view.frame.width
Swift:デバイスの回転後にUICollectionViewレイアウトをリフレッシュする方法
を使用して設定しますが、デバイスを回転するとセルは更新されません。
私は、姿勢変更時に呼び出される関数、発見した:私は、メインのコードがあるUICollectionViewレイアウト
を更新する方法を見つけることができません
override func willRotateToInterfaceOrientation(toInterfaceOrientation:
UIInterfaceOrientation, duration: NSTimeInterval) {
//code
}
けどをここに:
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
}}
おかげでしょうか?どのようにreloadDataを呼び出すと思いますか? –
UICollectionViewControllerにはすでに 'collectionView'プロパティがあります。それらを 'collectionView.reloadData()'に編集するだけです。それが動作します。 – Khuong
ビューを再読み込みする必要があるので、単にレイアウトの更新をトリガーすることはできません。 – Rivera