Hide Android Navigation Bar in React Native

后端 未结 7 1131
名媛妹妹
名媛妹妹 2020-12-10 02:27

How can I hide the Android Navigation Bar in React Native?

I\'m referring to the bar at the bottom of the screen that has the software back button and home button, n

相关标签:
7条回答
  • 2020-12-10 02:51

    If you're looking to achieve this permanently you'll have to hide the Navbar when the app is created and when it comes back into focus.

    To do so, add this in your MainActivity.java:

    ...
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends ReactActivity {
    
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            hideNavigationBar();
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus) {
                hideNavigationBar();
            }
        }
    
        private void hideNavigationBar() {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    }
    

    You may want to make it "immersive" so that the user can still access the navigation bar by pulling from the bottom of the screen. To do so, add the View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag:

    ...
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends ReactActivity {
    
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            hideNavigationBar();
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus) {
                hideNavigationBar();
            }
        }
    
        private void hideNavigationBar() {
            getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    
        }
    }
    

    You can find more options on the android documentation.

    0 讨论(0)
  • 2020-12-10 02:53

    There is no built-in API to do this from JavaScript.

    The good news is in React Native you can expose any native functionality to JavaScript, by writing a native module. See documentation.

    This way you can provide a JS function like NavigationBarAndroid.hide() and make it call the API you linked to.

    0 讨论(0)
  • 2020-12-10 02:55

    Building off of Martin Konicek's answer:

    I went ahead and wrote the Package and Module you need here: https://gist.github.com/dhrrgn/16a8dfa7581a682627c6

    You need to add it in your MainActivity.java file in the getPackages method, and send the package the ReactActivity object like this: new NavigationBarAndroidPackage(this)

    Note: The code is untested, but it should work for you (you need to change the package on the first line). I just used the example code provided in the link you sent as an example. Modify to your needs.

    0 讨论(0)
  • 2020-12-10 02:59

    I was stuck at the exact same point today. I found a medium article that got the job done. This question seems to be quite old however, some new readers might find this helpful.

    https://medium.com/@mykl.ashton/how-to-hide-the-android-pie-navigation-bar-in-react-native-895e34c9e41

    In this article, the writer has written native java methods to control the navigation bar and then used react native bridge to access the methods from react side.

    It's a long article but has worked fantastically for me! hope this helps!

    0 讨论(0)
  • 2020-12-10 03:01

    Use this: https://github.com/Anthonyzou/react-native-full-screen

    usage:

    import FullScreen from 'react-native-full-screen'
    FullScreen.onFullScreen()
    FullScreen.offFullScreen()
    
    0 讨论(0)
  • 2020-12-10 03:05

    To hide the Android Navigation bar you can do that using react-native-navigation-bar-color it allows you to show or hide the navigation bar. You can see more in the documentation [here][1]. Note that it will not work on Expo as it requires you to link native code.

    Installation is fairly straight forward:

    npm install react-native-navigation-bar-color --save
    

    Then you need to link the package:

    react-native link react-native-navigation-bar-color
    

    Once you have done that you can use it in the following way:

    import {
      HideNavigationBar,
      ShowNavigationBar,
    } from 'react-native-navigation-bar-color';
    

    Then when you want to show the navigation bar you just use

    ShowNavigationBar();
    

    And to hide it you can use:

    HideNavigationBar();
    
    0 讨论(0)
提交回复
热议问题