如何在 react-native iOS 中获得正确的屏幕宽度? [英] How to get the correct screen width in react-native iOS?

查看:72
本文介绍了如何在 react-native iOS 中获得正确的屏幕宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 iPhone 6s,react-native Dimensions.get("window").width 返回 375....这似乎远非正确.

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

推荐答案

375 是 iPhone 6 的点数.对于大多数事情,点数就可以了.但是如果你需要像素,你可以使用 React Native 提供的 PixelRatio API.

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.

例如,要以渲染像素为单位获取 iPhone 上的距离,您可以使用 Dimensions.get('window').width * PixelRatio.get(),它应该为 iPhone 返回 7506.

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.

这篇关于如何在 react-native iOS 中获得正确的屏幕宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆