I have a small app with only 4 view controllers and I want to change the layout when the user transitions to/from landscape. I figured out how to check for landscape, but the function lives in one viewController class and I am not clear how to get my other viewControllers to take action when the viewWillTransition fires. Do I put a version of that override function in each view controller class to do things specific to it? When I tried putting the override in both classes after a segue they BOTH fired and didn't feel like "right way to do it" and so I wanted to ask the experts.
Using the code below on app start I set a global bool for whether it is in landscape and I use viewWillTransition to detect changes.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
print("Will Transition to size \(size) from super view size \(self.view.frame.size)")
if (size.width > self.view.frame.size.width) {
print("Landscape")
inLandscapeMode = true
} else {
print("Portrait")
inLandscapeMode = false
}
if (size.width != self.view.frame.size.width) {
DispatchQueue.main.async {
// DO SOMETHING LIKE reload a Table/Collection view to ensure no dequeued cells have old constraints... self.tableView.reloadData()
}
}
}
func setUpFormatting(){
let size = UIScreen.main.bounds.size
print("On App Start size \(size)")
if size.width > size.height {
print("Landscape")
inLandscapeMode = true
} else {
print("Portrait")
inLandscapeMode = false
}
}
All of
You can get the device orientation in every view controller and setup views by that value