iOS 8 introduce new screen types that are usable in Storyboards and in Xibs, can I detect these types in code? If yes, how?
Here you can find more about it https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
The iOS interface environment for your app, including traits such as horizontal and vertical size class, display scale, and user interface idiom. iOS 8.0+ iPadOS 8.0+ Mac Catalyst 13.1+ tvOS 9.0+
To detect current device with iOS/Swift we can use UserInterfaceIdiom. It is an enum in swift, which tells which device is being used. The interface idiom provides multiple values in it's enum which are.
Yes you can, UIViewControllers now have a traitCollection
property which has information from the device idiom, to the current size classes and more ... Furthermore you can implement the method func traitCollectionDidChange(previousTraitCollection: UITraitCollection)
to get notifications when the size class (or any trait) has changed (as when the user rotates the device on an iphone). The properties of UITraitCollection that you are looking for are horizontalSizeClass
and verticalSizeClass
..Here is a reference
Hope that helps
You can also detect kind of device and its orientation using below extension:
extension UITraitCollection {
var isIpad: Bool {
return horizontalSizeClass == .regular && verticalSizeClass == .regular
}
var isIphoneLandscape: Bool {
return verticalSizeClass == .compact
}
var isIphonePortrait: Bool {
return horizontalSizeClass == .compact && verticalSizeClass == .regular
}
var isIphone: Bool {
return isIphoneLandscape || isIphonePortrait
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With