Я использовал UICollectionView (flowlayout) для создания простой компоновки. ширина для каждой ячейки установлена на ширину экрана, используя self.view.frame.width
Swift: как обновить макет UICollectionView после поворота устройства
, но когда я поворачиваю устройство, ячейки не обновляются.
Я нашел функцию, которая вызывается при изменении ориентации:
override func willRotateToInterfaceOrientation(toInterfaceOrientation:
UIInterfaceOrientation, duration: NSTimeInterval) {
//code
}
, но я не могу найти способ, чтобы обновить UICollectionView макет
Основной код является здесь:
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
}}
спасибо @ khuong291 что, если у меня нет цели моего CollectionView, и у меня есть UICollectionViewController? как я могу назвать reloadData? –
UICollectionViewController уже имеет свойство 'collectionView', поэтому вы просто вызываете их, как я уже сказал. Просто отредактируйте их в 'collectionView.reloadData()'. Это будет работать. – Khuong
Итак, вам нужно перезагрузить представление, вы не можете просто запустить обновление макета? – Rivera