With the release of the iPhone X, most layouts need to be updated to work correctly with the new display. In some cases, your layout just needs more padding at the top so your content isn’t underneath the notch, or you need to move controls away from the bottom where the home button used to be; or maybe your venturing into new waters and have decided to make new layouts specifically for the iPhone X. Whatever the case is, you need a way to tell if the current device is an iPhone X, so I’ll be going over a few options on how you can accomplish this.
Is this an iPhone X?
If you’ve already searched the web on this question, or have already thought about the easiest way to accomplish this, you’ve probably already ran into the option for checking the screen size. While this does work, this is not the best option and is only a temporary solution because while the iPhone X is the only current iPhone with a screen size of 375×812 or 1125×2436 rendered pixels, this could change in the near future.
Getting Device Info
A much better solution is to check the device codes, a full list can be found here https://gist.github.com/adamawolf/3048717.
Doing this can be tricky as the results are different between simulators and actual devices. Most people will point you to a plugin that handles this for you, but if your like me, I like to try and avoid bloating my apps with plugins if I can help it.
The class I put together does use two methods from the XLabs plugin https://github.com/XLabs/Xamarin-Forms-Labs, if your already using this plugin you’ll already have access to these methods. But since I don’t need all of the functionality provided by XLabs I only used what is in Xamarin-Forms-Labs/src/Platform/XLabs.Platform.iOS/Device/AppleDevice.cs
This class is pretty straight forward, there is a List of strings (iphonesWithNotch) that gets initialized in the constructor with the device codes for the iPhone X, with room to add new devices as the come out later on. The method deviceHasNotch will return true if the current device code matches one of the codes in the list iphonesWithNotch. This method will of course work on both simulators and real devices.
To use this method, start in the AppDelegate.cs file, inside the FinishedLaunching method.
The variable App.onIphoneX is a static bool defined in the Forms project in App.xaml.cs file so that you can use this variable when creating your views.