Pri*_*ank 15 ios flexbox react-native
我正在使用React-Native编写通用iPhone/iPad应用程序.但是当方向改变时,我正在努力正确地渲染我的视图.以下是js文件的源代码:
'use strict';
var React = require('react-native');
var {
Text,
View
} = React;
var CardView = require('./CardView');
var styles = React.StyleSheet.create({
container:{
flex:1,
backgroundColor: 'red'
}
});
class MySimpleApp extends React.Component {
render() {
return <View style={styles.container}/>;
}
}
React.AppRegistry.registerComponent('SimpleApp', () => MySimpleApp);
Run Code Online (Sandbox Code Playgroud)
这是它在Portrait中呈现的方式(这是正确的):

但是,当设备旋转时.红色视图不会相应旋转.

Pab*_*rde 18
最简单的方法是:
import React, { Component } from 'react';
import { Dimensions, View, Text } from 'react-native';
export default class Home extends Component {
constructor(props) {
super(props);
this.state = {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
}
this.onLayout = this.onLayout.bind(this);
}
onLayout(e) {
this.setState({
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
});
}
render() {
return(
<View
onLayout={this.onLayout}
style={{width: this.state.width}}
>
<Text>Layout width: {this.state.width}</Text>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*shu 14
在本地反应中响应方向变化非常简单.本机中的每个视图都有一个名为onLayout的侦听器,它会在方向更改时调用.我们只需要实现这一点.最好将维度存储在状态变量中并更新每个方向更改,以便在更改后重新呈现.另外,我们需要重新加载视图以响应方向更改.
import React, { Component } from "react";
import { StyleSheet, Text, View, Image, Dimensions } from "react-native";
var { height, width } = Dimensions.get("window");
export default class Com extends Component {
constructor() {
console.log("constructor");
super();
this.state = {
layout: {
height: height,
width: width
}
};
}
_onLayout = event => {
console.log(
"------------------------------------------------" +
JSON.stringify(event.nativeEvent.layout)
);
this.setState({
layout: {
height: event.nativeEvent.layout.height,
width: event.nativeEvent.layout.width
}
});
};
render() {
console.log(JSON.stringify(this.props));
return (
<View
style={{ backgroundColor: "red", flex: 1 }}
onLayout={this._onLayout}
>
<View
style={{
backgroundColor: "green",
height: this.state.layout.height - 10,
width: this.state.layout.width - 10,
margin: 5
}}
/>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
对于最新版本的React Native,方向更改不一定会触发onLayout,但会Dimensions提供更直接相关的事件:
class App extends Component {
constructor() {
super();
this.state = {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
};
Dimensions.addEventListener("change", (e) => {
this.setState(e.window);
});
}
render() {
return (
<View
style={{
width: this.state.width,
height: this.state.height,
}}
>
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此代码用于应用程序的根组件。如果在应用程序中更深入地使用它,则需要包括一个相应的removeEventListener调用。
您可以使用react-native-orientation来检测和执行方向更改。
var Orientation = require('react-native-orientation');
Run Code Online (Sandbox Code Playgroud)
还可以使用Dimension类,该类返回size(width,height)。
Dimensions.get('window')
Run Code Online (Sandbox Code Playgroud)
使用这些方法来适应方向
componentDidMount() {
Orientation.lockToPortrait(); //this will lock the view to Portrait
//Orientation.lockToLandscape(); //this will lock the view to Landscape
//Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations
// self = this;
console.log('componentDidMount');
Orientation.addOrientationListener(this._orientationDidChange);
}
componentWillUnmount() {
console.log('componentWillUnmount');
Orientation.getOrientation((err,orientation)=> {
console.log("Current Device Orientation: ", orientation);
});
Orientation.removeOrientationListener(this._orientationDidChange);
}
_orientationDidChange(orientation) {
console.log('Orientation changed to '+orientation);
console.log(self);
if (orientation == 'LANDSCAPE') {
//do something with landscape layout
screenWidth=Dimensions.get('window').width;
console.log('screenWidth:'+screenWidth);
} else {
//do something with portrait layout
screenWidth=Dimensions.get('window').width;
console.log('screenWidth:'+screenWidth);
}
self.setState({
screenWidth:screenWidth
});
}
Run Code Online (Sandbox Code Playgroud)
我也用了,但是它的性能太低了。
希望有帮助...
好的。我找到了这个问题的答案。需要在我们的视图控制器中实现以下内容并在其中调用刷新我们的 ReactNative 视图。
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
| 归档时间: |
|
| 查看次数: |
14369 次 |
| 最近记录: |