I have a code assessment and never think about to something like this; I have to display the data retrieved from an API in two different layouts :
For iPad; only in landscape mode and in a grid. I will do it with a UIcollectionView
For iPhone; only in portrait mode and in a list. I will do it with a UItableView or StackView.
The problem is how to implement the logic that populates the collection view or the table view from the view controller. I think I have to use the UITraitCollection but I know if its the better option.
Someone can help me? Thanks
You can get the device from this method
// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom
// 2. check the idiom
switch (deviceIdiom) {
case .pad:
print("iPad style UI")
case .phone:
print("iPhone and iPod touch style UI")
case .tv:
print("tvOS style UI")
default:
print("Unspecified UI idiom")
}
And For Orientation use this method:-
struct Orientation {
// indicate current device is in the LandScape orientation
static var isLandscape: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isLandscape
: UIApplication.shared.statusBarOrientation.isLandscape
}
}
// indicate current device is in the Portrait orientation
static var isPortrait: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isPortrait
: UIApplication.shared.statusBarOrientation.isPortrait
}
}
}