Swift: Comment actualiser la disposition UICollectionView après la rotation de l'appareil

J'ai utilisé UICollectionView (flowlayout) pour build une layout simple. la largeur de chaque cellule est définie sur la largeur de l'écran en utilisant self.view.frame.width

mais quand je tourne l'appareil, les cellules ne sont pas mises à jour.

entrez la description de l'image ici

J'ai trouvé une fonction qui est appelée lors du changement d'orientation:

 override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { //code } 

mais je suis incapable de find un moyen de mettre à jour la layout UICollectionView

Le code principal est ici:

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

Ajoutez cette fonction:

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() myCollection.collectionViewLayout.invalidateLayout() } 

Lorsque vous changez d'orientation, cette fonction est appelée.

La meilleure option consiste à appeler invalidateLayout() au lieu de reloadData() car cela ne forcera pas la recréation des cellules, donc les performances seront légèrement meilleures:

 override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() myCollection.collectionViewLayout.invalidateLayout() } 

vous pouvez mettre à jour votre layout UICollectionView en utilisant

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { if isLandscape { return CGSizeMake(yourLandscapeWidth, yourLandscapeHeight) } else { return CGSizeMake(yourNonLandscapeWidth, yourNonLandscapeHeight) } } 

Vous pouvez également l'invalider de cette manière.

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [self.collectionView.collectionViewLayout invalidateLayout]; }