最后我选择了这个:
export function isIphoneXorAbove() {
const dimen = Dimensions.get('window');
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTVOS &&
((dimen.height === 812 || dimen.width === 812) || (dimen.height === 896 || dimen.width === 896))
);
}
但所有的功劳都归功于此PR https://github.com/ptelad/react-native-iphone-x-helper/pull/9
EDIT:如果您希望此函数适用于 iPhone 11 和 iPhone 12 版本,您可以使用此函数:
function isIphoneWithNotch() {
const dimen = Dimensions.get('window');
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTV &&
(dimen.height === 780 ||
dimen.width === 780 ||
dimen.height === 812 ||
dimen.width === 812 ||
dimen.height === 844 ||
dimen.width === 844 ||
dimen.height === 896 ||
dimen.width === 896 ||
dimen.height === 926 ||
dimen.width === 926)
);
}
EDIT 2:最后,我采用了一种更干净的方法,使用反应本机设备信息 https://github.com/react-native-device-info/react-native-device-info
const deviceId = DeviceInfo.getDeviceId();
const iphonesWithNotch = [
'iPhone10,3',
'iPhone11,2',
'iPhone11,4',
'iPhone11,6',
'iPhone11,8',
'iPhone12,1',
'iPhone12,3',
'iPhone12,5',
'iPhone12,8',
'iPhone13,1',
'iPhone13,2',
'iPhone13,3',
'iPhone13,4',
'iPhone14,2',
'iPhone14,3',
'iPhone14,4',
'iPhone14,5',
'iPhone14,6',
'iPhone14,7',
'iPhone14,8',
];
const isIphoneWithNotch = iphonesWithNotch.includes(deviceId);
或者更简单:
let hasNotch = DeviceInfo.hasNotch();