Skip to content

Instantly share code, notes, and snippets.

@taimila
Last active September 16, 2021 05:44
Show Gist options
  • Save taimila/3ea014ebc58aea2e3e12a8f934853cfe to your computer and use it in GitHub Desktop.
Save taimila/3ea014ebc58aea2e3e12a8f934853cfe to your computer and use it in GitHub Desktop.
Detect iOS device and split screen situation on Xamarin.Forms
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace NamespaceOfYourApp
{
/// <summary>
/// Utility to get more information on screen configuration, type and size on iOS devices.
/// </summary>
public class Screen
{
double width;
/// <summary>
/// Initializes a new instance of the <see cref="T:Flexer.Screen"/> class.
/// Usually initalized from ContentPage.OnSizeAllocated(double width, double height)
/// method. Should be initalized with width given by OnSizeAllocated method.
/// </summary>
/// <param name="width">Width of the current page.</param>
public Screen(double width)
{
this.width = width;
}
Size ScreenSize
{
get
{
var display = DeviceDisplay.MainDisplayInfo;
var height = display.Height / display.Density;
return new Size(width, height);
}
}
public bool IsLandscape => ScreenSize.Width > ScreenSize.Height;
/// <summary>
/// Gets the configuration.
/// </summary>
/// <value>The configuration.</value>
public ScreenConfiguration Configuration
{
get
{
if (ScreenSize == iPhoneSE.PortraitScreenSize)
return ScreenConfiguration.iPhoneSE_Portrait;
else if (ScreenSize == iPhoneSE.LandscapeScreenSize)
return ScreenConfiguration.iPhoneSE_Landscape;
else if (ScreenSize == iPhone.PortraitScreenSize)
return ScreenConfiguration.iPhone_Portrait;
else if (ScreenSize == iPhone.LandscapeScreenSize)
return ScreenConfiguration.iPhone_Landscape;
else if (ScreenSize == iPhonePlus.PortraitScreenSize)
return ScreenConfiguration.iPhonePlus_Portrait;
else if (ScreenSize == iPhonePlus.LandscapeScreenSize)
return ScreenConfiguration.iPhonePlus_Landscape;
else if (ScreenSize == iPhoneX.PortraitScreenSize)
return ScreenConfiguration.iPhoneX_Portrait;
else if (ScreenSize == iPhoneX.LandscapeScreenSize)
return ScreenConfiguration.iPhoneX_Landscape;
else if (ScreenSize == iPhoneXSMax.PortraitScreenSize)
return ScreenConfiguration.iPhoneXSMax_Portrait;
else if (ScreenSize == iPhoneXSMax.LandscapeScreenSize)
return ScreenConfiguration.iPhoneXSMax_Landscape;
else if (ScreenSize == iPad.PortraitScreenSize)
return ScreenConfiguration.iPad_Fullscreen_Portrait;
else if (ScreenSize == iPad.LandscapeScreenSize)
return ScreenConfiguration.iPad_Fullscreen_Landscape;
else if (ScreenSize == iPad.PortraitNarrowSplitSize)
return ScreenConfiguration.iPad_NarrowerSplitScreen_Portrait;
else if (ScreenSize == iPad.PortraitWideSplitSize)
return ScreenConfiguration.iPad_WiderSplitScreen_Portrait;
else if (ScreenSize == iPad.LandscapeNarrowSplitSize)
return ScreenConfiguration.iPad_NarrowerSplitScreen_Landscape;
else if (ScreenSize == iPad.LandscapeHalfSplitSize)
return ScreenConfiguration.iPad_HalfSplitScreen_Landscape;
else if (ScreenSize == iPad.LandscapeWideSplitSize)
return ScreenConfiguration.iPad_WiderSplitScreen_Landscape;
else if (ScreenSize == iPadPro12.PortraitScreenSize)
return ScreenConfiguration.iPad12_Fullscreen_Portrait;
else if (ScreenSize == iPadPro12.LandscapeScreenSize)
return ScreenConfiguration.iPad12_Fullscreen_Landscape;
else if (ScreenSize == iPadPro12.PortraitNarrowSplitSize)
return ScreenConfiguration.iPad12_NarrowerSplitScreen_Portrait;
else if (ScreenSize == iPadPro12.PortraitWideSplitSize)
return ScreenConfiguration.iPad12_WiderSplitScreen_Portrait;
else if (ScreenSize == iPadPro12.LandscapeNarrowSplitSize)
return ScreenConfiguration.iPad12_NarrowerSplitScreen_Landscape;
else if (ScreenSize == iPadPro12.LandscapeHalfSplitSize)
return ScreenConfiguration.iPad12_HalfSplitScreen_Landscape;
else if (ScreenSize == iPadPro12.LandscapeWideSplitSize)
return ScreenConfiguration.iPad12_WiderSplitScreen_Landscape;
else if (ScreenSize == iPadPro11.PortraitScreenSize)
return ScreenConfiguration.iPad11_Fullscreen_Portrait;
else if (ScreenSize == iPadPro11.LandscapeScreenSize)
return ScreenConfiguration.iPad11_Fullscreen_Landscape;
else if (ScreenSize == iPadPro11.PortraitNarrowSplitSize)
return ScreenConfiguration.iPad11_NarrowerSplitScreen_Portrait;
else if (ScreenSize == iPadPro11.PortraitWideSplitSize)
return ScreenConfiguration.iPad11_WiderSplitScreen_Portrait;
else if (ScreenSize == iPadPro11.LandscapeNarrowSplitSize)
return ScreenConfiguration.iPad11_NarrowerSplitScreen_Landscape;
else if (ScreenSize == iPadPro11.LandscapeHalfSplitSize)
return ScreenConfiguration.iPad11_HalfSplitScreen_Landscape;
else if (ScreenSize == iPadPro11.LandscapeWideSplitSize)
return ScreenConfiguration.iPad11_WiderSplitScreen_Landscape;
else if (ScreenSize == iPadPro10.PortraitScreenSize)
return ScreenConfiguration.iPad10_Fullscreen_Portrait;
else if (ScreenSize == iPadPro10.LandscapeScreenSize)
return ScreenConfiguration.iPad10_Fullscreen_Landscape;
else if (ScreenSize == iPadPro10.PortraitNarrowSplitSize)
return ScreenConfiguration.iPad10_NarrowerSplitScreen_Portrait;
else if (ScreenSize == iPadPro10.PortraitWideSplitSize)
return ScreenConfiguration.iPad10_WiderSplitScreen_Portrait;
else if (ScreenSize == iPadPro10.LandscapeNarrowSplitSize)
return ScreenConfiguration.iPad10_NarrowerSplitScreen_Landscape;
else if (ScreenSize == iPadPro10.LandscapeHalfSplitSize)
return ScreenConfiguration.iPad10_HalfSplitScreen_Landscape;
else if (ScreenSize == iPadPro10.LandscapeWideSplitSize)
return ScreenConfiguration.iPad10_WiderSplitScreen_Landscape;
return ScreenConfiguration.Unknown;
}
}
/// <summary>
/// Statusbar height on iOS device.
/// </summary>
/// <value>The height of the status bar on iOS.</value>
public int StatusBarHeight
{
get
{
var c = Configuration;
if (HasNotch)
{
if (c == ScreenConfiguration.iPhoneXSMax_Portrait ||
c == ScreenConfiguration.iPhoneX_Portrait)
return 44;
else
return 0;
}
return 20;
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="T:Flexer.Screen"/> has notch.
/// </summary>
/// <value><c>true</c> if iOS device has notch; otherwise, <c>false</c>.</value>
public bool HasNotch
{
get
{
var c = Configuration;
return c == ScreenConfiguration.iPhoneXSMax_Portrait ||
c == ScreenConfiguration.iPhoneXSMax_Landscape ||
c == ScreenConfiguration.iPhoneX_Portrait ||
c == ScreenConfiguration.iPhoneX_Landscape;
}
}
/// <summary>
/// iPhone XS Max
/// </summary>
public static class iPhoneXSMax
{
public static int NativeWidth = 1242;
public static int NativeHeight = 2688;
public static int Width = 414;
public static int Height = 896;
public static int Scale = 3;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
}
// <summary>
/// iPhone XR
/// </summary>
public static class iPhoneXR
{
public static int NativeWidth = 828;
public static int NativeHeight = 1792;
public static int Width = 414;
public static int Height = 896;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
}
/// <summary>
/// iPhone X, iPhone XS
/// </summary>
public static class iPhoneX
{
public static int NativeWidth = 1125;
public static int NativeHeight = 2436;
public static int Width = 375;
public static int Height = 812;
public static int Scale = 3;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
}
/// <summary>
/// iPhone 6 Plus, iPhone 7 Plus, iPhone 8 Plus
/// </summary>
public static class iPhonePlus
{
public static int NativeWidth = 1080;
public static int NativeHeight = 1920;
public static int Width = 414;
public static int Height = 736;
public static int Scale = 3;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
}
/// <summary>
/// iPhone 6, iPhone 7, iPhone 8
/// </summary>
public static class iPhone
{
public static int NativeWidth = 750;
public static int NativeHeight = 1334;
public static int Width = 375;
public static int Height = 667;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
}
/// <summary>
/// iPhone 5, iPhone 5S, iPhone SE
/// </summary>
public static class iPhoneSE
{
public static int NativeWidth = 640;
public static int NativeHeight = 1136;
public static int Width = 320;
public static int Height = 568;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
}
/// <summary>
/// iPad Mini (all), iPad 9.7" (all)
/// </summary>
public static class iPad
{
public static int NativeWidth = 1536;
public static int NativeHeight = 2048;
public static int Width = 768;
public static int Height = 1024;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
public static Size PortraitNarrowSplitSize = new Size(320, Height);
public static Size PortraitWideSplitSize = new Size(438, Height);
public static Size LandscapeNarrowSplitSize = new Size(320, Width);
public static Size LandscapeHalfSplitSize = new Size(507, Width);
public static Size LandscapeWideSplitSize = new Size(694, Width);
}
/// <summary>
/// iPad Pro 10.5"
/// </summary>
public static class iPadPro10
{
public static int NativeWidth = 1668;
public static int NativeHeight = 2224;
public static int Width = 834;
public static int Height = 1112;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
public static Size PortraitNarrowSplitSize = new Size(320, Height);
public static Size PortraitWideSplitSize = new Size(504, Height);
public static Size LandscapeNarrowSplitSize = new Size(320, Width);
public static Size LandscapeHalfSplitSize = new Size(551, Width);
public static Size LandscapeWideSplitSize = new Size(782, Width);
}
/// <summary>
/// iPad Pro 11"
/// </summary>
public static class iPadPro11
{
public static int NativeWidth = 1668;
public static int NativeHeight = 2388;
public static int Width = 834;
public static int Height = 1194;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
public static Size PortraitNarrowSplitSize = new Size(320, Height);
public static Size PortraitWideSplitSize = new Size(504, Height);
public static Size LandscapeNarrowSplitSize = new Size(375, Width);
public static Size LandscapeHalfSplitSize = new Size(592, Width);
public static Size LandscapeWideSplitSize = new Size(809, Width);
}
/// <summary>
/// iPad Pro 12.9"
/// </summary>
public static class iPadPro12
{
public static int NativeWidth = 2048;
public static int NativeHeight = 2732;
public static int Width = 1024;
public static int Height = 1366;
public static int Scale = 2;
public static Size PortraitScreenSize => new Size(Width, Height);
public static Size LandscapeScreenSize => new Size(Height, Width);
public static Size PortraitNarrowSplitSize = new Size(320, Height);
public static Size PortraitWideSplitSize = new Size(694, Height);
public static Size LandscapeNarrowSplitSize = new Size(375, Width);
public static Size LandscapeHalfSplitSize = new Size(678, Width);
public static Size LandscapeWideSplitSize = new Size(981, Width);
}
}
}
public enum ScreenConfiguration
{
Unknown,
iPhoneSE_Portrait,
iPhoneSE_Landscape,
iPhone_Portrait,
iPhone_Landscape,
iPhonePlus_Portrait,
iPhonePlus_Landscape,
iPhoneX_Portrait, // and XS
iPhoneX_Landscape, // and XS
iPhoneXSMax_Portrait, // and XR
iPhoneXSMax_Landscape, // and XR
iPad_Fullscreen_Portrait,
iPad_NarrowerSplitScreen_Portrait,
iPad_WiderSplitScreen_Portrait,
iPad_Fullscreen_Landscape,
iPad_NarrowerSplitScreen_Landscape,
iPad_HalfSplitScreen_Landscape,
iPad_WiderSplitScreen_Landscape,
iPad10_Fullscreen_Portrait,
iPad10_NarrowerSplitScreen_Portrait,
iPad10_WiderSplitScreen_Portrait,
iPad10_Fullscreen_Landscape,
iPad10_NarrowerSplitScreen_Landscape,
iPad10_HalfSplitScreen_Landscape,
iPad10_WiderSplitScreen_Landscape,
iPad11_Fullscreen_Portrait,
iPad11_NarrowerSplitScreen_Portrait,
iPad11_WiderSplitScreen_Portrait,
iPad11_Fullscreen_Landscape,
iPad11_NarrowerSplitScreen_Landscape,
iPad11_HalfSplitScreen_Landscape,
iPad11_WiderSplitScreen_Landscape,
iPad12_Fullscreen_Portrait,
iPad12_NarrowerSplitScreen_Portrait,
iPad12_WiderSplitScreen_Portrait,
iPad12_Fullscreen_Landscape,
iPad12_NarrowerSplitScreen_Landscape,
iPad12_HalfSplitScreen_Landscape,
iPad12_WiderSplitScreen_Landscape,
}
@logikonline
Copy link

Thank you 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment