Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock orientation for specific screen on TabNavigator

I used TabNavigation for my app. In some screen, I want it only display on portrait orientation, and others are landscape. I've found react-native-orientation and try in my screens:

Screen 1

import React from "react";
import Orientation from 'react-native-orientation';
import { Text } from 'react-native'

export default Screen1 extends React.PureComponent{

 componentDidMount(){
  Orientation.lockToLandscape();
 }

 componentWillUnmount(){
  Orientation.unlockAllOrientations();
 }

 render() {
  return(<Text>Screen 1</Text>);
 }
}

Screen 2

import React from "react";
import Orientation from 'react-native-orientation';
import { Text } from 'react-native'

export default Screen2 extends React.PureComponent{

 componentDidMount(){
  Orientation.lockToPortrait();
 }

 componentWillUnmount(){
  Orientation.unlockAllOrientations();
 }

 render() {
  return(<Text>Screen 2</Text>);
 }
}

Tab Navigator

const AppNavigator = TabNavigator(  {
  Screen1: { screen: Screen1 },
  Screen2: { screen: Screen2 },
});

But it always portrait, which mean that its orientation always set base on orientation of last screen I add in TabNavigator. Any help is appreciate, thank you in advance! Edit 1 I've try StackNavigation instead, and it work. Still don't know why it's not run with TabNavigation.

like image 982
tuan.tran Avatar asked Feb 05 '18 11:02

tuan.tran


People also ask

How do I lock the orientation of the screen?

Well, here is a compilation of how to lock the screen orientation that I have found all over the Internet: 1 Use the Javascript screen.orientation.lock () API function. 2 Use CSS transform: rotate (90 deg) on the orientation that you don’t want. 3 Show a “please rotate screen” message on the orientation that you don’t want.

How to lock screen orientation in flutter?

How to Lock Screen Orientation In Flutter? This will give you access to the SystemChrome class, which “Controls specific aspects of the operating system’s graphical interface and how it interacts with the application.” To block rotation in the entire app implement PortraitModeMixin in the main App widget.

How to rotate screen orientation in JavaScript?

Use the Javascript screen.orientation.lock () API function. Use CSS transform: rotate (90 deg) on the orientation that you don’t want. Show a “please rotate screen” message on the orientation that you don’t want. Just how does each one of these work? Let’s walk through some examples, read on to find out!

How do I block rotation of the screen in Android?

To block rotation in the entire app implement PortraitModeMixin in the main App widget. Remember to call super.build (context) in Widget build (BuildContext context) method. To block rotation in a specific screen implement PortraitStatefulModeMixin < SampleScreen > in the specific screen’s state.


1 Answers

It has been long time since I asked this questions, and react-navigation is upgrade to version 3. My solution for this problem is set orientation for tab each time it's pressed, cause tab screen isn't unmount when you click on other tab. To fix that, you can add tabBarOnPress to your navigationOptions of your screen like this

import Orientation from 'react-native-orientation';
...
class TabbarScreen1 extends Component {
  static navigationOptions = {
    ...,
    tabBarOnPress: ({ defaultHandler }) => {
      // Orientation.lockToLandscape();
      Orientation.lockToPortrait();
      defaultHandler();
    },
  }
  ...
}
like image 76
tuan.tran Avatar answered Oct 19 '22 03:10

tuan.tran