Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I detect if screen is regular or compact from code on iOS 8?

Tags:

ios

ios8

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

like image 317
Tomáš Linhart Avatar asked Sep 11 '14 18:09

Tomáš Linhart


People also ask

What is Traitcollection?

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+

How can I tell if my Iphone is swift or IPAD?

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.


2 Answers

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

like image 98
Daniel Avatar answered Oct 11 '22 03:10

Daniel


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
    }
}
like image 7
Bartłomiej Semańczyk Avatar answered Oct 11 '22 03:10

Bartłomiej Semańczyk