【问题标题】:How to lock orientation to Portrait in iOS React Native App?如何在 iOS React Native App 中将方向锁定为纵向?
【发布时间】:2018-07-31 09:28:29
【问题描述】:

我正在尝试取消勾选“设备方向设置”,留下纵向选项,但它不起作用。在 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>

【问题讨论】:

  • 在 iOS 文件夹中的 Info.plist 文件中,尝试删除:UIInterfaceOrientationLandscapeLeft 和 UIInterfaceOrientationLandscapeRight
  • 我的 Info.plist 文件中没有这些键:S
  • 奇怪!你能检查下ios文件夹>你的项目名称文件夹>info.plist
  • 会像&lt;key&gt;UISupportedInterfaceOrientations&lt;/key&gt; &lt;array&gt; &lt;string&gt;UIInterfaceOrientationPortrait&lt;/string&gt; &lt;string&gt;UIInterfaceOrientationLandscapeLeft&lt;/string&gt; &lt;string&gt;UIInterfaceOrientationLandscapeRight&lt;/string&gt; &lt;/array&gt;
  • 检查我的 Info.plist,我编辑了问题。

标签: ios react-native mobile orientation


【解决方案1】:

终于,我找到了答案。这不是我期望的解决方案,因为我认为锁定XCode 就足够了,正如我之前在询问中提到的那样,但至少它有效。

我安装了react-native-orientation 并在App.js 文件的构造函数上应用了Orientation.lockToPortrait() 函数,并将应用程序锁定为纵向。

希望对你有帮助!

【讨论】:

  • 在问题发生之前你是否已经有了 react-native-orientation?可能是这个库实际上弄乱了锁定配置。
  • @Benoit 不,我没有。
【解决方案2】:

请执行以下步骤以在 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 就好了。

【讨论】:

  • 所有这些都是我做过的,没有用。正如我所说,我取消了所有不是“纵向”的可能性,应用程序没有任何变化,它仍在旋转。
  • @Mtg Kha Jeskai,我可以知道部署信息中“设备”的值是什么吗?
  • @Mtg Kha Jeskai,正如我之前所说,如果“设备”设置为通用,方向锁定似乎在 iPad/iPhone 上不起作用。尝试将其更改为 iPad 或 iPhone。
  • 对不起,我没有阅读更新。我尝试将字段从“通用”更改为“iPhone”,尽管只标记了“纵向”选项,但它仍在旋转。 T_T
【解决方案3】:

我和你有同样的问题,原来是因为我的AppDelegate.m中配置了react-native-orientation

如果我取消注释这 2 个配置

#import "Orientation.h"

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {return [Orientation getOrientation];}

然后它工作正常。但是如果你使用 react-native-orientation,这些配置是必要的。所以我认为最终的解决方案是使用Orientation.lockToPortrait()

【讨论】:

  • 是的,这对我来说很好。 react-native-orientation 阻止了 xcode 配置。从我的 appdelegate.m 中删除这些行后,通过 xcode 锁定就可以了。
【解决方案4】:

尝试清理项目,重建应用程序,也许会有所帮助。一旦我无法锁定设备方向,唯一的方法就是勾选纵向选项。

【讨论】:

  • 我做了,但我没有任何变化:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多