How to know the useful height of an iOS device in React Native?












1















In some very specific cases I need to set the height of a View to the full height of the device useful area (without using flex).



I was using a hardcoded "notch height" to calculate this useful height but I just discovered that the notch can have different heights depending on the device. (3 points of difference between iPhone XS and iPhone XS Max).



Is there a way to know the useful height of a device with notch and safe area?










share|improve this question



























    1















    In some very specific cases I need to set the height of a View to the full height of the device useful area (without using flex).



    I was using a hardcoded "notch height" to calculate this useful height but I just discovered that the notch can have different heights depending on the device. (3 points of difference between iPhone XS and iPhone XS Max).



    Is there a way to know the useful height of a device with notch and safe area?










    share|improve this question

























      1












      1








      1








      In some very specific cases I need to set the height of a View to the full height of the device useful area (without using flex).



      I was using a hardcoded "notch height" to calculate this useful height but I just discovered that the notch can have different heights depending on the device. (3 points of difference between iPhone XS and iPhone XS Max).



      Is there a way to know the useful height of a device with notch and safe area?










      share|improve this question














      In some very specific cases I need to set the height of a View to the full height of the device useful area (without using flex).



      I was using a hardcoded "notch height" to calculate this useful height but I just discovered that the notch can have different heights depending on the device. (3 points of difference between iPhone XS and iPhone XS Max).



      Is there a way to know the useful height of a device with notch and safe area?







      ios reactjs react-native react-native-android react-native-ios






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 12:55









      JPHortaJPHorta

      7341919




      7341919
























          4 Answers
          4






          active

          oldest

          votes


















          1














          You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.



          import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'

          //Retrieve safe area insets for root view

          SafeArea.getSafeAreaInsetsForRootView()
          .then((result) => {
          console.log(result)
          // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } }
          })





          share|improve this answer
























          • Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

            – JPHorta
            Nov 19 '18 at 13:08



















          2














          You can get the screen, which users phone, width and height from Dimensions component.



          import { Dimensions } from 'react-native'

          const { width, height } = Dimensions.get('window'); // if you use the width you can get the screen width by pixels. And also height is the height pixels of the phone.


          const screenWidthSomePart = width * 0,6 // Some times you can get the percentage of the screen so you can use this. screen %60



          If you wanna see the safe are for the Iphone X. You can use the SafeAreaView Componenet



          import { SafeAreaView } from 'react-native'
          return(
          <SafeAreaView>
          ..... // your screen componenet
          </SafeAreaView>
          );





          share|improve this answer





















          • 1





            In iPhone XS the window height includes the size of the notch

            – JPHorta
            Nov 19 '18 at 12:03



















          1














          As @mohammed-ashfaq said, react-native-safe-area solves the problem. However, it returns the insets with a promise and I needed those values statically.



          Given that, I created react-native-static-safe-area-insets that enables access to the insets values as constants.






          share|improve this answer































            0














            Use Dimension module from 'react-native' like there :



            import { Dimensions, Platform, StatusBar } from 'react-native'

            const windowHeight = Dimensions.get('window').height
            const screenHeight = Dimensions.get('screen').height





            share|improve this answer
























            • In iPhone XS the window and screen height are the same and include the size of the notch

              – JPHorta
              Nov 19 '18 at 12:02











            • Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

              – JPHorta
              Nov 19 '18 at 15:29











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319968%2fhow-to-know-the-useful-height-of-an-ios-device-in-react-native%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.



            import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'

            //Retrieve safe area insets for root view

            SafeArea.getSafeAreaInsetsForRootView()
            .then((result) => {
            console.log(result)
            // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } }
            })





            share|improve this answer
























            • Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

              – JPHorta
              Nov 19 '18 at 13:08
















            1














            You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.



            import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'

            //Retrieve safe area insets for root view

            SafeArea.getSafeAreaInsetsForRootView()
            .then((result) => {
            console.log(result)
            // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } }
            })





            share|improve this answer
























            • Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

              – JPHorta
              Nov 19 '18 at 13:08














            1












            1








            1







            You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.



            import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'

            //Retrieve safe area insets for root view

            SafeArea.getSafeAreaInsetsForRootView()
            .then((result) => {
            console.log(result)
            // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } }
            })





            share|improve this answer













            You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.



            import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'

            //Retrieve safe area insets for root view

            SafeArea.getSafeAreaInsetsForRootView()
            .then((result) => {
            console.log(result)
            // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } }
            })






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 13:24









            Mohammed AshfaqMohammed Ashfaq

            1,6792612




            1,6792612













            • Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

              – JPHorta
              Nov 19 '18 at 13:08



















            • Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

              – JPHorta
              Nov 19 '18 at 13:08

















            Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

            – JPHorta
            Nov 19 '18 at 13:08





            Thanks! This works nicely. My only issue now is the return of the getSafeAreaInsetsForRootView method being a promise, given that I want to use this to set some style variables that are exported for the whole app.

            – JPHorta
            Nov 19 '18 at 13:08













            2














            You can get the screen, which users phone, width and height from Dimensions component.



            import { Dimensions } from 'react-native'

            const { width, height } = Dimensions.get('window'); // if you use the width you can get the screen width by pixels. And also height is the height pixels of the phone.


            const screenWidthSomePart = width * 0,6 // Some times you can get the percentage of the screen so you can use this. screen %60



            If you wanna see the safe are for the Iphone X. You can use the SafeAreaView Componenet



            import { SafeAreaView } from 'react-native'
            return(
            <SafeAreaView>
            ..... // your screen componenet
            </SafeAreaView>
            );





            share|improve this answer





















            • 1





              In iPhone XS the window height includes the size of the notch

              – JPHorta
              Nov 19 '18 at 12:03
















            2














            You can get the screen, which users phone, width and height from Dimensions component.



            import { Dimensions } from 'react-native'

            const { width, height } = Dimensions.get('window'); // if you use the width you can get the screen width by pixels. And also height is the height pixels of the phone.


            const screenWidthSomePart = width * 0,6 // Some times you can get the percentage of the screen so you can use this. screen %60



            If you wanna see the safe are for the Iphone X. You can use the SafeAreaView Componenet



            import { SafeAreaView } from 'react-native'
            return(
            <SafeAreaView>
            ..... // your screen componenet
            </SafeAreaView>
            );





            share|improve this answer





















            • 1





              In iPhone XS the window height includes the size of the notch

              – JPHorta
              Nov 19 '18 at 12:03














            2












            2








            2







            You can get the screen, which users phone, width and height from Dimensions component.



            import { Dimensions } from 'react-native'

            const { width, height } = Dimensions.get('window'); // if you use the width you can get the screen width by pixels. And also height is the height pixels of the phone.


            const screenWidthSomePart = width * 0,6 // Some times you can get the percentage of the screen so you can use this. screen %60



            If you wanna see the safe are for the Iphone X. You can use the SafeAreaView Componenet



            import { SafeAreaView } from 'react-native'
            return(
            <SafeAreaView>
            ..... // your screen componenet
            </SafeAreaView>
            );





            share|improve this answer















            You can get the screen, which users phone, width and height from Dimensions component.



            import { Dimensions } from 'react-native'

            const { width, height } = Dimensions.get('window'); // if you use the width you can get the screen width by pixels. And also height is the height pixels of the phone.


            const screenWidthSomePart = width * 0,6 // Some times you can get the percentage of the screen so you can use this. screen %60



            If you wanna see the safe are for the Iphone X. You can use the SafeAreaView Componenet



            import { SafeAreaView } from 'react-native'
            return(
            <SafeAreaView>
            ..... // your screen componenet
            </SafeAreaView>
            );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 '18 at 12:12

























            answered Nov 15 '18 at 13:18









            Yasin UgurluYasin Ugurlu

            21528




            21528








            • 1





              In iPhone XS the window height includes the size of the notch

              – JPHorta
              Nov 19 '18 at 12:03














            • 1





              In iPhone XS the window height includes the size of the notch

              – JPHorta
              Nov 19 '18 at 12:03








            1




            1





            In iPhone XS the window height includes the size of the notch

            – JPHorta
            Nov 19 '18 at 12:03





            In iPhone XS the window height includes the size of the notch

            – JPHorta
            Nov 19 '18 at 12:03











            1














            As @mohammed-ashfaq said, react-native-safe-area solves the problem. However, it returns the insets with a promise and I needed those values statically.



            Given that, I created react-native-static-safe-area-insets that enables access to the insets values as constants.






            share|improve this answer




























              1














              As @mohammed-ashfaq said, react-native-safe-area solves the problem. However, it returns the insets with a promise and I needed those values statically.



              Given that, I created react-native-static-safe-area-insets that enables access to the insets values as constants.






              share|improve this answer


























                1












                1








                1







                As @mohammed-ashfaq said, react-native-safe-area solves the problem. However, it returns the insets with a promise and I needed those values statically.



                Given that, I created react-native-static-safe-area-insets that enables access to the insets values as constants.






                share|improve this answer













                As @mohammed-ashfaq said, react-native-safe-area solves the problem. However, it returns the insets with a promise and I needed those values statically.



                Given that, I created react-native-static-safe-area-insets that enables access to the insets values as constants.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 13:27









                JPHortaJPHorta

                7341919




                7341919























                    0














                    Use Dimension module from 'react-native' like there :



                    import { Dimensions, Platform, StatusBar } from 'react-native'

                    const windowHeight = Dimensions.get('window').height
                    const screenHeight = Dimensions.get('screen').height





                    share|improve this answer
























                    • In iPhone XS the window and screen height are the same and include the size of the notch

                      – JPHorta
                      Nov 19 '18 at 12:02











                    • Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

                      – JPHorta
                      Nov 19 '18 at 15:29
















                    0














                    Use Dimension module from 'react-native' like there :



                    import { Dimensions, Platform, StatusBar } from 'react-native'

                    const windowHeight = Dimensions.get('window').height
                    const screenHeight = Dimensions.get('screen').height





                    share|improve this answer
























                    • In iPhone XS the window and screen height are the same and include the size of the notch

                      – JPHorta
                      Nov 19 '18 at 12:02











                    • Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

                      – JPHorta
                      Nov 19 '18 at 15:29














                    0












                    0








                    0







                    Use Dimension module from 'react-native' like there :



                    import { Dimensions, Platform, StatusBar } from 'react-native'

                    const windowHeight = Dimensions.get('window').height
                    const screenHeight = Dimensions.get('screen').height





                    share|improve this answer













                    Use Dimension module from 'react-native' like there :



                    import { Dimensions, Platform, StatusBar } from 'react-native'

                    const windowHeight = Dimensions.get('window').height
                    const screenHeight = Dimensions.get('screen').height






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 13:11









                    Vincent MenantVincent Menant

                    18917




                    18917













                    • In iPhone XS the window and screen height are the same and include the size of the notch

                      – JPHorta
                      Nov 19 '18 at 12:02











                    • Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

                      – JPHorta
                      Nov 19 '18 at 15:29



















                    • In iPhone XS the window and screen height are the same and include the size of the notch

                      – JPHorta
                      Nov 19 '18 at 12:02











                    • Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

                      – JPHorta
                      Nov 19 '18 at 15:29

















                    In iPhone XS the window and screen height are the same and include the size of the notch

                    – JPHorta
                    Nov 19 '18 at 12:02





                    In iPhone XS the window and screen height are the same and include the size of the notch

                    – JPHorta
                    Nov 19 '18 at 12:02













                    Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

                    – JPHorta
                    Nov 19 '18 at 15:29





                    Looking at RCTDeviceInfo.m, line 75 reads: typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.

                    – JPHorta
                    Nov 19 '18 at 15:29


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319968%2fhow-to-know-the-useful-height-of-an-ios-device-in-react-native%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith