How to design iPad Landscape and Portrait screens with different Layouts using Size class. I could find only w-regular and h-regular for both orientations. Example: I need t
Swift 4
override func overrideTraitCollection(forChildViewController childViewController: UIViewController) -> UITraitCollection? {
if UIDevice.current.userInterfaceIdiom == .pad && UIDevice.current.orientation.isLandscape {
return UITraitCollection(traitsFrom:[UITraitCollection(verticalSizeClass: .compact), UITraitCollection(horizontalSizeClass: .regular)])
}
return super.overrideTraitCollection(forChildViewController: childViewController)
}
I like to create a custom subclass of navigationController and then set a storyboards initial Navigation controller to that class. You can also do something similar with a ViewController.
Example:
import UIKit
class NavigationControllerWithTraitOverride: UINavigationController {
// If you make a navigationController a member of this class the descendentVCs of that navigationController will have their trait collection overridden with compact vertical size class if the user is on an iPad and the device is horizontal.
override func overrideTraitCollection(forChildViewController childViewController: UIViewController) -> UITraitCollection? {
if UIDevice.current.userInterfaceIdiom == .pad && UIDevice.current.orientation.isLandscape {
return UITraitCollection(traitsFrom:[UITraitCollection(verticalSizeClass: .compact), UITraitCollection(horizontalSizeClass: .regular)])
}
return super.overrideTraitCollection(forChildViewController: childViewController)
}
}
Note: You should not override traitCollection as per the docs
Important
Use the traitCollection property directly. Do not override it. Do not provide a custom implementation.