简体   繁体   中英

How to detect iPhone X home indicator?

The new iPhone X has gotten rid of the home button and replaced it with a "home indicator" at the very bottom that allows the user to swipe up to get back to the home screen.

My question is, how do we detect if this home indicator is on the screen? I want to do something like:

if (!notfullScreen)
{
    if (swipeBarExists)
    {
        viewHeight -= swipeBarHeight;
    }
}

I checked in [UIApplication sharedApplication] and found nothing. Basically I don't really know what to call this thing and am having trouble finding an answer.

You can utilize safeAreaInsets.bottom , defined for UIView to get the amount of space you should inset your content to ensure it doesn't underlap the home indicator (or other elements). Note that this value may change, for example, when you rotate to landscape on iPhone it shrinks. To be notified when this occurs, implement safeAreaInsetsDidChange in your view controller. You can also utilize the safeAreaLayoutGuide with Auto Layout.

So, if you have a full screen view, you could check like so:

override func viewSafeAreaInsetsDidChange() {
    super.viewSafeAreaInsetsDidChange()

    if view.safeAreaInsets.bottom > 0 {
        //home indicator
    } else {
       //no home indicator
    }
}

Note that there is no API to get the height of just the home indicator bar itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM