How to get the correct screen width in react-native iOS?

蓝咒 提交于 2021-02-18 21:11:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!