加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:45:53 所属栏目:百科 来源:网络整理
导读:react-native Dimensions.get(“window”).宽度返回375 for iPhone 6s …. 这似乎远非正确. ` import React,{ Component } from 'react';import { AppRegistry,StyleSheet,Text,View,Dimensions} from 'react-native';class AwesomeProject extends Componen
react-native Dimensions.get(“window”).宽度返回375 for iPhone 6s ….
这似乎远非正确.

`

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.

例如,要获得渲染像素中iPhone的距离,您可以使用Dimensions.get(‘window’).width * PixelRatio.get(),它应该为iPhone 6返回750.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读