skip to Main Content

I want to disable rotation only at a certain screen – not to the whole app, how can I do that?

2

Answers


  1. I think you are working with react native project. you can control screen orientation on a per-screen basis using the react-native-orientation-locker library.which allows you to lock the orientation of specific screens while leaving the rest of the app unaffected.

    Here’s how you can disable rotation for a specific screen:

    1.Install the library using npm

    npm install react-native-orientation-locker
    
    1. Link the Library (if React Native versions older than 0.60)
    react-native link react-native-orientation-locker
    
    1. Configure the Orientation in a Specific Screen
      You can use the OrientationLocker component to lock the orientation on a specific screen as below (just example).

    lock the orientation on a specific screen

    import React from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    import Orientation from 'react-native-orientation-locker';
    
    const HomeScreen = () => {
      React.useEffect(() => {
        // Lock the orientation when the screen is mounted
        Orientation.lockToPortrait(); // You can also use lockToLandscape()
    
        // Unlock orientation when the screen is unmounted
        return () => Orientation.unlockAllOrientations();
      }, []);
    
      return (
        <View style={styles.container}>
          <Text>Home Screen with Portrait Orientation</Text>
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      },
    });
    
    export default HomeScreen;
    

    Allow Rotation in Other Screens

    import React from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    
    const OtherScreen = () => {
      return (
        <View style={styles.container}>
          <Text>Other Screen with Default Orientation</Text>
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      },
    });
    
    export default OtherScreen;
    
    1. Update Android and iOS Configurations

    Android

    • Ensure the android:screenOrientation is not globally locked in your AndroidManifest.xml for the app.

    Ios

    • Make sure the supported orientations are set in your Info.plist file and not globally locked.
    Login or Signup to reply.
  2. If react-native-orientation-locker did not work for you, you can restrict orientation by using Modal, you can use the supportedOrientations prop in React Native’s Modal component.

    <Modal supportedOrientations={['portrait']}>
          {/* content */}
    </Modal>
    

    This ensures that the modal only supports portrait orientation, even if the rest of the app supports rotation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search