[英]How to lock orientation to Portrait in iOS React Native App?
我试图取消勾选“设备方向设置”,只勾选了“纵向”选项,但它不起作用。 在Android中,它不会旋转,但在iOS中,它旋转,我也不知道为什么。 无论我在哪个窗口中,它都会在我的整个应用程序中旋转。 您知道锁定方向引起的旋转的其他方法吗?
这是我的Info.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>NEXT PORTAL</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>7.7</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>12</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) necesita acceder a su cámara para poder escanear el código QR.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSMainNibFile</key>
<string>LaunchScreen</string>
<key>NSMainNibFile~ipad</key>
<string>LaunchScreen</string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) would like to your microphone (for videos)</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) necesita acceder a la galería para adjuntar fotos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) necesita acceder a la galería para adjuntar fotos.</string>
<key>UIAppFonts</key>
<array>
<string>Entypo.ttf</string>
<string>EvilIcons.ttf</string>
<string>Feather.ttf</string>
<string>FontAwesome.ttf</string>
<string>Foundation.ttf</string>
<string>Ionicons.ttf</string>
<string>MaterialCommunityIcons.ttf</string>
<string>MaterialIcons.ttf</string>
<string>Octicons.ttf</string>
<string>SimpleLineIcons.ttf</string>
<string>Zocial.ttf</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UIRequiresFullScreen</key>
<true/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
请执行以下步骤以在React Native iOS App中将方向锁定为纵向:
1] Open Your_React_Native_Project -> go to ios -> project_name.xcodeproj file in X-Code.
2] After opening the project in Xcode, select your project name.
3] Now Goto -> General -> Deployment Info and select device orientation as portrait.
4] Execute the "react-native run-ios" command and compile the entire project. Now you can see the effect.
另外还观察到,如果将“设备”设置为“通用”,则方向锁定在iPad上似乎不起作用。 如果将其更改为iPad,则可以正常工作。
最后,我找到了答案。 这不是我所期望的解决方案,因为我认为通过锁定XCode
就足够了,就像我之前在问中提到的那样,但是至少它可以工作。
我安装了react-native-orientation
并在App.js
文件的构造函数中应用了Orientation.lockToPortrait()
函数,它将应用程序锁定为纵向。
希望对您有所帮助!
我也遇到了同样的问题,事实证明原因是因为我的AppDelegate.m中配置了react-native-orientation
如果我取消注释这2个配置
#import "Orientation.h"
和
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {return [Orientation getOrientation];}
然后工作正常。 但是,如果您使用的是本机定向,则必须进行这些配置。 所以我认为最终的解决方案是使用Orientation.lockToPortrait()
尝试清理项目,重建应用程序,也许有帮助。 一旦我无法锁定设备方向,唯一的方法就是勾选纵向选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.