前提:
import {
Platform,
Dimensions
} from 'react-native';
RN 判断 iPhone X
// iPhoneX
const X_WIDTH = 375;
const X_HEIGHT = 812;
// screen
const SCREEN_WIDTH = Dimensions.get('window').width;
const SCREEN_HEIGHT = Dimensions.get('window').height;
export function isIphoneX() {
return (
Platform.OS === 'ios' &&
((SCREEN_HEIGHT === X_HEIGHT && SCREEN_WIDTH === X_WIDTH) ||
(SCREEN_HEIGHT === X_WIDTH && SCREEN_WIDTH === X_HEIGHT))
)
}
export function ifIphoneX (iphoneXStyle, regularStyle) {
if (isIphoneX()) {
return iphoneXStyle;
} else {
return regularStyle
}
}
使用:
footer: {
position: 'absolute',
width: '100%',
height: 64,
alignItems: 'center',
justifyContent: 'center',
borderColor: 'lightgray',
borderTopWidth: 1,
...ifIphoneX({
bottom: 20,
}, {
bottom: 0,
})
},