Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 961
Lawrence Cheuk Avatar asked Aug 29 '16 17:08

Lawrence Cheuk


1 Answers

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.

like image 147
Michael Helvey Avatar answered Nov 13 '22 20:11

Michael Helvey