Getting the size of the notch...

Hi all,


I am making some existing game apps compatible with iPhone X. All these games are simply using a fillscreen window and have all their elements drawn manually in OpenGL. I want to make sure buttons and things avoid the notch. I'm not using any storyboards or anything fancy, the initialization is very lightweight.


How can I get the size of the notch? I'm using Objective-C. I'm just looking for a simplest possible call that I can use to say "okay, keep all game UI elements to the right of pixel position x."


Thanks!

Accepted Answer

Okay, simple enough. Is there a nice easy Obj-C call to get that safe area? Remember, I just have a full screen view, and I draw everywhere-- I just want to get the values of the margins that let me stay inside it.


(Edit) Answered my own question...


UIWindow* window=[[UIApplication sharedApplication] keyWindow];

float reservedTop=window.safeAreaInsets.top;

float reservedBottom=window.safeAreaInsets.bottom;

float reservedLeft=window.safeAreaInsets.left;

float reservedRight=window.safeAreaInsets.right

Answers

Detailed dimensions can be found in an image approx. 1/2 way down the page here:


h ttps://uxdesign.cc/designing-for-the-iphone-x-part-1-6cbc7f30734d

Is there no method of getting the size as reported by Apple? It would be convenient if the app would adapt itself to future devices where the notch size might be different.

Apple wants devs to obey 'safe areas' that avoid the notch zone entirely (thus future proofing in the bargain), treating the top of the screen as a margin, vs. your plan to share that area by staying left/right,


See: https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/

Okay, simple enough. Is there a nice easy Obj-C call to get that safe area? Remember, I just have a full screen view, and I draw everywhere-- I just want to get the values of the margins that let me stay inside it.


(Edit) Answered my own question...


UIWindow* window=[[UIApplication sharedApplication] keyWindow];

float reservedTop=window.safeAreaInsets.top;

float reservedBottom=window.safeAreaInsets.bottom;

float reservedLeft=window.safeAreaInsets.left;

float reservedRight=window.safeAreaInsets.right

You may want to check for iOS11 availablity:


float iPhoneXCorrection=0.0;
if (@available(iOS 11.0, *)){
        iPhoneXCorrection=[UIApplication sharedApplication].delegate.window.safeAreaInsets.top;
        // or your code and you could use your keyWindow rather than delegate.window
}