问题
react-native Dimensions.get("window").width return 375 for an iPhone 6s.... it seems far from correct.
`
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Dimensions
} from 'react-native';
class AwesomeProject extends Component {
render() {
return <Text style={{margin:50}}>Width: {Dimensions.get("window").width}</Text>;
}
}
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);`
回答1:
375 is the number of points across for the iPhone 6. For most things, points will be fine. But if you need pixels, you can use the PixelRatio
API provided by React Native.
For example, to get the distance across the iPhone in rendered pixels you could use Dimensions.get('window').width * PixelRatio.get()
, which should return 750 for the iPhone 6.
来源:https://stackoverflow.com/questions/39211518/how-to-get-the-correct-screen-width-in-react-native-ios