Disabling Screen Rotation in React Native Applications

Disabling Screen Rotation in React Native Applications

17 December 2024 Stephan Petzl Leave a comment Tech-Help

Screen orientation management is a common requirement in mobile app development, particularly when you want to maintain a consistent user interface. In this guide, we will explore how to disable screen rotation in React Native applications for both iOS and Android platforms.

Disabling Rotation in iOS

For iOS applications, managing screen orientation is straightforward and can be achieved directly through Xcode:

  • Open your project in Xcode.
  • Navigate to the General tab of your target settings.
  • In the Device Orientation section, uncheck Landscape Left and Landscape Right to restrict the app to portrait mode only.

These settings ensure your application supports only the selected orientations, effectively disabling rotation.

Disabling Rotation in Android

On the Android side, you need to modify the AndroidManifest.xml file:

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
  android:windowSoftInputMode="adjustResize"
  android:screenOrientation="portrait">

  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

This configuration locks the screen orientation to portrait mode for the specified activity.

Using Expo for Orientation Management

If you are using Expo to build your React Native app, you can configure orientation through the app.json file, which simplifies the process for both iOS and Android:

{
  "expo": {
    "name": "My app",
    "slug": "my-app",
    "sdkVersion": "21.0.0",
    "privacy": "public",
    "orientation": "portrait"
  }
}

This configuration automatically applies the orientation settings during build time, reducing the need for platform-specific adjustments.

Enhancing Your Development Workflow

Developing robust mobile applications often requires efficient testing and debugging. Tools like Repeato, a no-code test automation solution, can significantly streamline this process. Repeato allows you to create, run, and maintain automated tests for your mobile applications without writing a single line of code. Its computer vision and AI capabilities ensure fast and reliable test execution, making it an ideal choice for React Native projects.

For further reading on related topics, you may explore our articles on resolving simulator issues in Xcode or triggering events upon animation completion in React Native.

Like this article? there’s more where that came from!