I have created the most simple of Apps, Xcode 9.2, New Project/Single View App/Next/Next etc. and replaced the ViewController with a CollectionViewController.
This is the entire code of the CollectionViewController, and note self.collectionView!.backgroundColor = UIColor.yellow to set the background color to yellow.
This works fine. When I rotate the iPhone in the simulator, it does not seem to invalidate the ViewController, as seen by yellow not extending the entire width of the phone when the phone is horizontal.
Here is my code.
import UIKit
class CollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.backgroundColor = UIColor.yellow
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("test-1")
let x1 = self.collectionViewLayout;
//x1.invalidateLayout() //uncomment this line
print("test-2")
}
}
I thought I understood this, and I searched the Apple forums and StackOverflow too (here is a really relevant prior posting), and it appears I need to invalidate the collection view manually in code.
If I remove the comment from line //x1.invalidateLayout() xCode prints out in the output window "test-1" and "test-2" in an infinite loop.
A pointer to push me into the right direction here would be most welcome. Clearly when the iPhone is rotated in the simulator, horizontally it should cover the entire screen in yellow too.
Thank you.