I would like to build a collectionView that holds all the cells in place when the user rotates the device without locking the device's orientation into portrait mode. I have tried invalidating the layout but to no avail. Thank you for your help.
Based on your comment, that you want to keep all screens in portrait orientation, but know if the device is in landscape orientation without rotating the interface (for a camera screen), you should…
your info.plist
detect the device orientation using CMMotionManager
…
let motionManager = CMMotionManager()
var currentOrientation = UIDeviceOrientation.portrait
func montiorOrientation() {
motionManager.startAccelerometerUpdates(to: OperationQueue()) { data, error in
if let error = error {
print("CM orientation error - \(error)")
return
}
let acceleration = data!.acceleration
let orientation: UIDeviceOrientation = fabs(acceleration.y) < fabs(acceleration.x) ?
(acceleration.x > 0 ? .landscapeRight : .landscapeLeft) :
(acceleration.y > 0 ? self.currentOrientation : .portrait) // Ignore portraitUpsideDown
if orientation != self.currentOrientation {
DispatchQueue.main.async {
self.currentOrientation = orientation
}
}
}
}