Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether an iOS device has a home bar using JS or CSS

I am building a web app for iOS, and want to know if it's possible using JS or CSS to detect whether a device has the on-screen Home Bar at the bottom (i.e. an iPhone X, 11, or iPad Pro - with rounded corners and no home button).

Because if creating a tab bar, I need to know whether or not to add the additional padding at the bottom to accommodate this, as illustrated in the image below.

enter image description here

Short of using media queries to detect from a list of EXACT screen resolutions for these devices and hoping for the best, I can't come up with a solution.

Any ideas out there?

like image 801
Alex Avatar asked Aug 21 '20 23:08

Alex


People also ask

How do I find my iPhone using JavaScript?

Here's how you can check whether someone on your website or app is using a mobile iOS device in JavaScript: const ios = () => { if (typeof window === `undefined` || typeof navigator === `undefined`) return false; return /iPhone|iPad|iPod/i. test(navigator. userAgent || navigator.

What is iOS home indicator?

The home indicator (If I'm getting the name correct) is that little bar at the bottom of an iPhone screen that you swipe up in order to return to the home screen. This bar replaced the home button / finger print scanner of older models.

What is home bar iOS?

Anthony Bouchard ∙ May 19, 2020. If your iPhone doesn't have a Home Button, then it probably has a Home Bar instead. The Home Bar provides a convenient pathway for accessing the Home Screen, App Switcher, Reachability, and other imperative things, but one thing Apple appears to have left out is customization.


1 Answers

Working from this post, I was able to calculate the height of the bottom safe area, thus determining whether the home bar is present. If you return a non-zero pixel value, the home bar is (probably) there. If it returns 0px, no home bar.

Definitely read the linked post, but the general idea is that you'll set the CSS env() function to the :root element and then read the calculated value back.

Here's how it works:

First, you'll need to use the whole available screen by putting this in the <head> section of your document:

<meta name="viewport" content="viewport-fit=cover" />

Then, add to your CSS:

:root {
    --sat: env(safe-area-inset-top);
    --sar: env(safe-area-inset-right);
    --sab: env(safe-area-inset-bottom); /* THIS ONE GETS US THE HOME BAR HEIGHT */
    --sal: env(safe-area-inset-left);
}

Finally, read back the value in Javascript:

getComputedStyle(document.documentElement).getPropertyValue("--sab")

This should return a value like 34px when the home bar is present and 0px when it ain't.

like image 116
Sam Avatar answered Sep 20 '22 10:09

Sam