react-native-orientation github地址:react-native-orientation ;
安裝步驟:
npm i --save react-native-orientation
react-native link react-native-orientation (自動link不成功践图,建議手動link)
使用方法:
在某個component引入 import Orientation from 'react-native-orientation‘
某個頁面強制橫屏:
componentWillMount() {
Orientation.lockToLandscapeRight();
//Orientation.lockToLandscape();
//Orientation.lockToLandscapeLeft();
}
那么問題來了,如果系統(tǒng)屏蔽橫屏以上設置不起作用,該怎么解決癣猾?
如圖這兩個選項取消勾選,Orientation.lockToLandscape() 不起作用
landscape.png
這個時候我們需要在ios內做一些修改:
1.打開xcode找到默認的AppDelegate.m文件
AppDelegate.m.png
2.在文件頭部引入Orientation.h文件
Orientation.h.png
#import "Orientation.h"
3.添加以下代碼后重新build坦康,運行即可
Orientation.png
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return [Orientation getOrientation];
}