Listen to device orientation changes in react-native. Orientation module adapted from @clavery.
- Run
npm install react-native-orientation --save
- Open your project in XCode, right click on your project and click
Add Files to "Your Project Name"
- Add
RCTOrientation
from yournode_modules/react-native-orientation
folder. - Whenever you want to use it within React code now you can:
var Orientation = require('react-native-orientation');
_orientationDidChange: function(orientation) {
if(orientation == 'LANDSCAPE'){
//do something with landscape layout
}else{
//do something with portrait layout
}
},
componentDidMount: function(){
Orientation.addOrientationListener(this._orientationDidChange);
},
componentWillUnmount: function() {
Orientation.removeOrientationListener(this._orientationDidChange);
}
addOrientationListener(function)
removeOrientationListener(function)
_orientationDidChange(orientation)
orientation can return either LANDSCAPE
PORTRAIT
UNKNOWN
- Add some way to allow setting a preferred orientation on a screen by screen basis.