How to detect iPhone X through code

I was just playing around with the iPhone X simulator in Xcode, and I realized one of my apps has a view bug. I have a button placed with code (at the bottom of the screen in the middle) that is overlapping the bar that you swipe up to get to the home screen. Is there a way to detect, through code, if the device is an iPhone X, and an iPhone X only, so I can write code to counteract the bug? Maybe by detecting the precise screen width and height, and if so, what are those numbers? Thanks in advance. Any help is appreciated.

Accepted Reply

I'll join the chorus recommending to not do this for screen layout purposes - but for the record, sniffing LABiometryType should allow device detection/bifurcation:


https://developer.apple.com/documentation/localauthentication/labiometrytype

Replies

The best thing to do is to use Autolayout. I did zero changes for iPhone X for all my apps (which use a single storyboard for all devices). If you are already using Autolayout, or cannot use it (e.g. some games have different requirements), then check out this video for pointers: https://developer.apple.com/videos/play/fall2017/801/


Screen resolution is not really a good thing to check since some devices have two resolutions (based upon if Display Zoom is 'zoomed' vs. 'standard').

I am already using AutoLayout for my UI elements that don’t move, but the button I am talking about needs to be placed with code. I also modify the width and height when certain actions occur such as touching down on the button. Is there any other way to detect the device?

Take a look at this github gist - https://gist.github.com/Jaybles/1323251

That would work for me if my app was in Objective-C, but is there a version of that in Swift?

I'll join the chorus recommending to not do this for screen layout purposes - but for the record, sniffing LABiometryType should allow device detection/bifurcation:


https://developer.apple.com/documentation/localauthentication/labiometrytype