Reputation: 21
I have two view controllers; one for landscape and another for portrait . How can I switch these programmatically when the device rotates, just like the native iPhone calculator.
Also, the portrait and landscape view controllers are different from eachother in xcode and swift.
Any help would be appreciated.
Upvotes: 1
Views: 2622
Reputation: 1830
Sometimes its necessary to display another controller. NOTE: .compact trait are all iPhones landscape. I did it this way:
lazy var switchViewController: UIViewController? = {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SwitchViewController")
vc?.modalTransitionStyle = .crossDissolve
return vc
}()
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (_) in
let orientation = newCollection.verticalSizeClass
switch orientation {
case .compact:
self.present(self.switchViewController!, animated: true, completion: .none)
default:
self.switchViewController!.dismiss(animated: true, completion: .none)
}
}, completion: .none)
super.willTransition(to: newCollection, with: coordinator)
}
Upvotes: 1
Reputation: 4772
The modern way to handle this kind of thing is by having one controller that adapts itself to different sizes, to handle not only the wild variety of possible screen sizes but the iPad's Slide Over and Split View multitasking. Where you should make significant changes to the UI like the Calculator example is by Size Class differentiation, explained here:
Storyboards in Interface Builder by default use size classes. Size classes are traits assigned to user interface elements, like scenes or views. They provide a rough indication of the element’s size. Interface Builder lets you customize many of your layout’s features based on the current size class. The layout then automatically adapts as the size class changes. Specifically, you can set the following features on a per-size-class basis:
- Install or uninstall a view or control.
- Install or uninstall a constraint.
- Set the value of select attributes (for example, fonts and layout margin settings).
When the system loads a scene, it instantiates all the views, controls, and constraints, and assigns these items to the appropriate outlet in the view controller (if any). You can access any of these items through their outlets, regardless of the scene’s current size class. However, the system adds these items to the view hierarchy only if they are installed for the current size class.
As the view’s size class changes (for example, when you rotate an iPhone or switch an iPad app between full-screen and Split View), the system automatically adds items to or removes them from the view hierarchy. The system also animates any changes to the view’s layout.
Excellent tutorials can be found at raywenderlich.com:
Upvotes: 1
Reputation: 464
You don't need to use different UIViewControllers for these.Only you have to do is override some methods of UIViewController to modify UI when device orientation is changed.
You can follow These links:
tutorial 1
tutorial 2
Upvotes: 0