Dimensions.get('screen').width returns iPad 模拟器中的宽度错误?
Dimensions.get('screen').width returns wrong width in iPad simulator?
Dimensions.get('screen').width
为我的 iPad simulator
返回宽度 320
。
No matter which iPad device I try to use in the simulator, it returns
a 320 x 480 screen and window resolution
。
我是不是做错了什么,或者这在模拟器中不能正常工作?
react-native 宽高的单位是密度无关像素,不是普通像素
为什么要使用density-independent pixels?
智能手机设备尺寸太多。无论屏幕尺寸如何,组件都应该看起来相同。
Setting dimensions this way is common for components that should
always render at exactly the same size, regardless of screen
dimensions.
在大多数情况下,您可以只使用 Layout with Flexbox。
感谢@Dani Akash,我想我找到了一个解决方案,可以满足我在这里要做的事情。 React Native: How to Determine if Device is iPhone or iPad
建议是使用纵横比来确定它是否是平板电脑。
import { Dimensions } from 'react-native';
const {height, width} = Dimensions.get('window');
const aspectRatio = height/width;
if(aspectRatio>1.6) {
// Code for Iphone
}
else {
// Code for Ipad
}
我知道这是事后的,但您也可以使用 react-native-device-info,它有一个方法 isTablet()。我不确定您使用的是哪个版本的 RN,但是 Dimensions.get('window') 应该可以正常工作,我认为问题可能是您使用的是 'screen' 而不是 'window'.
Dimensions.get('screen').width
为我的 iPad simulator
返回宽度 320
。
No matter which iPad device I try to use in the simulator, it returns a 320 x 480 screen and window resolution
。
我是不是做错了什么,或者这在模拟器中不能正常工作?
react-native 宽高的单位是密度无关像素,不是普通像素
为什么要使用density-independent pixels?
智能手机设备尺寸太多。无论屏幕尺寸如何,组件都应该看起来相同。
Setting dimensions this way is common for components that should always render at exactly the same size, regardless of screen dimensions.
在大多数情况下,您可以只使用 Layout with Flexbox。
感谢@Dani Akash,我想我找到了一个解决方案,可以满足我在这里要做的事情。 React Native: How to Determine if Device is iPhone or iPad
建议是使用纵横比来确定它是否是平板电脑。
import { Dimensions } from 'react-native';
const {height, width} = Dimensions.get('window');
const aspectRatio = height/width;
if(aspectRatio>1.6) {
// Code for Iphone
}
else {
// Code for Ipad
}
我知道这是事后的,但您也可以使用 react-native-device-info,它有一个方法 isTablet()。我不确定您使用的是哪个版本的 RN,但是 Dimensions.get('window') 应该可以正常工作,我认为问题可能是您使用的是 'screen' 而不是 'window'.