Two methods for React Native to detect whether an iOS device has a notch

React Native Apr 10, 2021 Viewed 3K Comments 0

Since iOS 11, iPhone notch has appeared. Use React Native to check whether iOS device has notch. Two methods are introduced below.

1. According to the device model

Use the hasNotch() method of react-native-device-info to tells if the device has a notch. It is suitable for Android and iOS. Check the source code of deviceinfo.js, and find the hasNotch() method. We can see the devicesWithNotch variable, which lists all brands and models with notch. The code is as follows:

const devicesWithNotch = [
  {
    brand: 'Apple',
    model: 'iPhone 11',
  },
  {
    brand: 'Apple',
    model: 'iPhone 11 Pro',
  },
  {
    brand: 'Apple',
    model: 'iPhone 11 Pro Max',
  },
  {
    brand: 'Apple',
    model: 'iPhone X',
  },
  {
    brand: 'Apple',
    model: 'iPhone XS',
  },
  {
    brand: 'Apple',
    model: 'iPhone XS Max',
  },
  {
    brand: 'Apple',
    model: 'iPhone XR',
  },
  {
    brand: 'Asus',
    model: 'ZenFone 5',
  },
  {
    brand: 'Asus',
    model: 'ZenFone 5z',
  },
  {
    brand: 'google',
    model: 'Pixel 3 XL',
  },
  {
    brand: 'Huawei',
    model: 'P20',
  },
  {
    brand: 'Huawei',
    model: 'P20 Plus',
  },
  {
    brand: 'Huawei',
    model: 'P20 Lite',
  },
  {
    brand: 'Huawei',
    model: 'ANE-LX1',
  },
  {
    brand: 'Huawei',
    model: 'INE-LX1',
  },
  {
    brand: 'Huawei',
    model: 'Honor 10',
  },
  {
    brand: 'Huawei',
    model: 'Mate 20 Lite',
  },
  {
    brand: 'Huawei',
    model: 'Mate 20 Pro',
  },
  {
    brand: 'Huawei',
    model: 'P30 Lite',
  },
  {
    brand: 'Huawei',
    model: 'P30 Pro',
  },
  {
    brand: 'Huawei',
    model: 'Nova 3',
  },
  {
    brand: 'Huawei',
    model: 'Nova 3i',
  },
  {
    brand: 'Leagoo',
    model: 'S9',
  },
  {
    brand: 'LG',
    model: 'G7',
  },
  {
    brand: 'LG',
    model: 'G7 ThinQ',
  },
  {
    brand: 'LG',
    model: 'G7+ ThinQ',
  },
  {
    brand: 'LG',
    model: 'LM-Q910', //G7 One
  },
  {
    brand: 'LG',
    model: 'LM-G710', //G7 ThinQ
  },
  {
    brand: 'LG',
    model: 'LM-V405', //V40 ThinQ
  },
  {
    brand: 'Motorola',
    model: 'Moto g7 Play',
  },
  {
    brand: 'Motorola',
    model: 'Moto g7 Power',
  },
  {
    brand: 'Motorola',
    model: 'One',
  },
  {
    brand: 'Nokia',
    model: '5.1 Plus',
  },
  {
    brand: 'Nokia',
    model: '6.1 Plus',
  },
  {
    brand: 'Nokia',
    model: '7.1',
  },
  {
    brand: 'Nokia',
    model: '8.1',
  },
  {
    brand: 'OnePlus',
    model: '6',
  },
  {
    brand: 'OnePlus',
    model: 'A6003',
  },
  {
    brand: 'ONEPLUS',
    model: 'A6000',
  },
  {
    brand: 'OnePlus',
    model: 'OnePlus A6003',
  },
  {
    brand: 'OnePlus',
    model: 'ONEPLUS A6010',
  },
  {
    brand: 'OnePlus',
    model: 'ONEPLUS A6013',
  },
  {
    brand: 'OnePlus',
    model: 'ONEPLUS A6000',
  },
  {
    brand: 'Oppo',
    model: 'R15',
  },
  {
    brand: 'Oppo',
    model: 'R15 Pro',
  },
  {
    brand: 'Oppo',
    model: 'F7',
  },
  {
    brand: 'Oukitel',
    model: 'U18',
  },
  {
    brand: 'Sharp',
    model: 'Aquos S3',
  },
  {
    brand: 'Vivo',
    model: 'V9',
  },
  {
    brand: 'Vivo',
    model: 'X21',
  },
  {
    brand: 'Vivo',
    model: 'X21 UD',
  },
  {
    brand: 'xiaomi',
    model: 'MI 8',
  },
  {
    brand: 'xiaomi',
    model: 'MI 8 Explorer Edition',
  },
  {
    brand: 'xiaomi',
    model: 'MI 8 SE',
  },
  {
    brand: 'xiaomi',
    model: 'MI 8 UD',
  },
  {
    brand: 'xiaomi',
    model: 'MI 8 Lite',
  },
  {
    brand: 'xiaomi',
    model: 'POCO F1',
  },
  {
    brand: 'xiaomi',
    model: 'POCOPHONE F1',
  },
  {
    brand: 'xiaomi',
    model: 'Redmi 6 Pro',
  },
  {
    brand: 'xiaomi',
    model: 'Redmi Note 7',
  },
  {
    brand: 'xiaomi',
    model: 'Mi A2 Lite',
  },
];

2. Custom iOS Native Modules

Create a iOS Native Modules, check whether the value of window.safeAreaInsets.bottom is greater than 0, export the constant, the code is as follows.

  • Objective C

NotchNative.h

//  NotchNative.h

#import <React/RCTBridgeModule.h>
@interface NotchNative : NSObject <RCTBridgeModule>

@end

NotchNative.m

// NotchNative.m

#import "NotchNative.h"
#import <UIKit/UIKit.h>

@implementation NotchNative

// To export a module named RCTCalendarModule
RCT_EXPORT_MODULE();

- (NSDictionary *)constantsToExport
{
  NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
  BOOL isBangsScreen = NO;
  CGFloat topPadding = 0;
  CGFloat bottomPadding = 0;
  if (@available(iOS 11.0, *)) {
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    topPadding = window.safeAreaInsets.top;
    bottomPadding = window.safeAreaInsets.bottom;
    isBangsScreen = bottomPadding > 0;
  }
  [dictionary setObject:[NSNumber numberWithInt:isBangsScreen] forKey:@"isBangsScreen"];
  [dictionary setObject:[NSNumber numberWithFloat:topPadding] forKey:@"topPadding"];
  [dictionary setObject:[NSNumber numberWithFloat:bottomPadding] forKey:@"bottomPadding"];
  return dictionary;
}

@end
  • Reactjs
import { NativeModules, Platform } from 'react-native'

if (Platform.OS === "ios") {
    const notchModule = NativeModules.NotchNative;
    console.log({notchModule});
    const data = {
        isBangsScreen: notchModule.isBangsScreen,
        topPadding: notchModule.topPadding,
        bottomPadding: notchModule.bottomPadding
    };
    console.log(data);
}
Updated Apr 10, 2021