Package Exports
- react-native-orientation
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-orientation) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-native-orientation
Listen to device orientation changes in react-native and set preferred orientation on screen to screen basis. Orientation listener adapted from @clavery. Preferred orientation on screen to screen basis adapted from @dsibiski rnplay-ios.
Add it to your project
- 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
RCTOrientationfrom yournode_modules/react-native-orientationfolder. - Open AppDelegate.h and add the following property above UIWindow
@property (nonatomic) bool shouldRotate; - Open AppDelegate.m and add the following before the
@end
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSUInteger orientations;
orientations = UIInterfaceOrientationMaskPortrait;
if (shouldRotate == YES) {
orientations = UIInterfaceOrientationMaskAllButUpsideDown;
}
return orientations;
}Whenever you want to use it within React Native code now you can:
var Orientation = require('react-native-orientation');
Usage
_orientationDidChange: function(orientation) {
if(orientation == 'LANDSCAPE'){
//do something with landscape layout
}else{
//do something with portrait layout
}
},
componentDidMount: function(){
Orientation.shouldRotate(1); //this will allow rotation
Orientation.addOrientationListener(this._orientationDidChange);
},
componentWillUnmount: function() {
Orientation.removeOrientationListener(this._orientationDidChange);
}Events
addOrientationListener(function)
removeOrientationListener(function)
Functions
shouldRotate(BOOL)
shouldRotate value should be either 0 or 1
_orientationDidChange(orientation)
orientation can return either LANDSCAPE PORTRAIT UNKNOWN
TODOS
- Add some way to allow setting a preferred orientation on a screen by screen basis.