如何在react-native iOS中获得正确的屏幕宽度?

时间:2016-08-29 17:05:57

标签: react-native width

react-native Dimensions.get(“window”)。宽度为iPhone 6s返回375 .... 这似乎远非正确。

`

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);`

clearly wrong, but no one reported this issue in their github, so it seems I did it wrongly....

1 个答案:

答案 0 :(得分:19)

375是iPhone 6的的数量。对于大多数情况,积分都没问题。但是,如果您需要像素,则可以使用React Native提供的PixelRatio API。

例如,要以渲染像素获取iPhone上的距离,您可以使用Dimensions.get('window').width * PixelRatio.get(),iPhone 6将返回750.