Getting navigation bar height in dependency service - Xamarin Forms





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















i have this issue wherein i need to get the navigation bar height in my Dependency Service.



Currently I am stuck on what to follow here. I tried everything i find in stackoverflow and google but no one works for me.



Heres my code:



[assembly: Dependency(typeof(DeviceInfo))]
namespace Wicket.App.Mobile.iOS.Framework
{
public class DeviceInfo : IDeviceInfo
{
public float StatusBarHeight => (float)UIApplication.SharedApplication.StatusBarFrame.Size.Height;

public float NavigationBarHeight => GetNavigationBarHeight();

public static UINavigationController NavigationController { get; set; }

public float GetNavigationBarHeight()
{
//Get navigation bar height


return 0;
}
}
}


I already completed the android part and it works good. The only problem now is in iOS. I have tried getting the instance of navigationcontroller in AppDelegate so that I can just get the bar frame like this NavigationBar.Bounds.Height;










share|improve this question





























    0















    i have this issue wherein i need to get the navigation bar height in my Dependency Service.



    Currently I am stuck on what to follow here. I tried everything i find in stackoverflow and google but no one works for me.



    Heres my code:



    [assembly: Dependency(typeof(DeviceInfo))]
    namespace Wicket.App.Mobile.iOS.Framework
    {
    public class DeviceInfo : IDeviceInfo
    {
    public float StatusBarHeight => (float)UIApplication.SharedApplication.StatusBarFrame.Size.Height;

    public float NavigationBarHeight => GetNavigationBarHeight();

    public static UINavigationController NavigationController { get; set; }

    public float GetNavigationBarHeight()
    {
    //Get navigation bar height


    return 0;
    }
    }
    }


    I already completed the android part and it works good. The only problem now is in iOS. I have tried getting the instance of navigationcontroller in AppDelegate so that I can just get the bar frame like this NavigationBar.Bounds.Height;










    share|improve this question

























      0












      0








      0








      i have this issue wherein i need to get the navigation bar height in my Dependency Service.



      Currently I am stuck on what to follow here. I tried everything i find in stackoverflow and google but no one works for me.



      Heres my code:



      [assembly: Dependency(typeof(DeviceInfo))]
      namespace Wicket.App.Mobile.iOS.Framework
      {
      public class DeviceInfo : IDeviceInfo
      {
      public float StatusBarHeight => (float)UIApplication.SharedApplication.StatusBarFrame.Size.Height;

      public float NavigationBarHeight => GetNavigationBarHeight();

      public static UINavigationController NavigationController { get; set; }

      public float GetNavigationBarHeight()
      {
      //Get navigation bar height


      return 0;
      }
      }
      }


      I already completed the android part and it works good. The only problem now is in iOS. I have tried getting the instance of navigationcontroller in AppDelegate so that I can just get the bar frame like this NavigationBar.Bounds.Height;










      share|improve this question














      i have this issue wherein i need to get the navigation bar height in my Dependency Service.



      Currently I am stuck on what to follow here. I tried everything i find in stackoverflow and google but no one works for me.



      Heres my code:



      [assembly: Dependency(typeof(DeviceInfo))]
      namespace Wicket.App.Mobile.iOS.Framework
      {
      public class DeviceInfo : IDeviceInfo
      {
      public float StatusBarHeight => (float)UIApplication.SharedApplication.StatusBarFrame.Size.Height;

      public float NavigationBarHeight => GetNavigationBarHeight();

      public static UINavigationController NavigationController { get; set; }

      public float GetNavigationBarHeight()
      {
      //Get navigation bar height


      return 0;
      }
      }
      }


      I already completed the android part and it works good. The only problem now is in iOS. I have tried getting the instance of navigationcontroller in AppDelegate so that I can just get the bar frame like this NavigationBar.Bounds.Height;







      xamarin.forms xamarin.ios






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 15:02









      Secret CoderSecret Coder

      835935




      835935
























          3 Answers
          3






          active

          oldest

          votes


















          0














          I think this should work:



          var navheight = GetTopViewController().NavigationController.NavigationBar.Frame.Height;


          public static UIViewController GetTopViewController()
          {
          var window = UIApplication.SharedApplication.KeyWindow;
          var vc = window.RootViewController;
          while (vc.PresentedViewController != null)
          vc = vc.PresentedViewController;

          if (vc is UINavigationController navController)
          vc = navController.ViewControllers.Last();

          return vc;
          }





          share|improve this answer


























          • i already tried that and it crash my app or it resullts to null

            – Secret Coder
            Nov 16 '18 at 15:28













          • What error you got?

            – Nirmal Subedi
            Nov 16 '18 at 15:29











          • its always result to null on NavigationController part. not i use the dependency on my Page constructor

            – Secret Coder
            Nov 16 '18 at 15:30













          • How is your Navigation Starts ?

            – Nirmal Subedi
            Nov 16 '18 at 15:33











          • it started from a master detail page

            – Secret Coder
            Nov 16 '18 at 15:45



















          0














          Solution:



          How about pass an instance of viewController as parameter in the function inside the IDeviceInfo?



          Try this:



           public void getNaviHeight(ContentPage vc)
          {
          var renderer = Platform.GetRenderer(vc);
          if (renderer == null)
          {
          renderer = RendererFactory.GetRenderer(vc);
          Platform.SetRenderer(vc, renderer);
          }
          var viewController = renderer.ViewController;

          var h = viewController.NavigationController?.NavigationBar.Frame.Height;

          }


          And use the dependency:



          public MainPage ()
          {
          DependencyService.Get<IDeviceInfo>().getNaviHeight(this);
          }





          share|improve this answer
























          • not working. NavigatioController always null

            – Secret Coder
            Nov 21 '18 at 17:47











          • Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

            – Jack Hua - MSFT
            Nov 22 '18 at 3:09











          • I get a ViewController but the NavigationController is always null

            – Secret Coder
            Nov 22 '18 at 3:13











          • So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

            – Jack Hua - MSFT
            Nov 22 '18 at 3:17











          • Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

            – Secret Coder
            Nov 22 '18 at 6:12



















          0














          this worked to me:



          var navigationBar = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Subviews[0].Subviews.OfType<UINavigationBar>().FirstOrDefault();

          if(navigationBar != null)
          {
          // continue here...
          }





          share|improve this answer
























            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%2f53340354%2fgetting-navigation-bar-height-in-dependency-service-xamarin-forms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I think this should work:



            var navheight = GetTopViewController().NavigationController.NavigationBar.Frame.Height;


            public static UIViewController GetTopViewController()
            {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;

            if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();

            return vc;
            }





            share|improve this answer


























            • i already tried that and it crash my app or it resullts to null

              – Secret Coder
              Nov 16 '18 at 15:28













            • What error you got?

              – Nirmal Subedi
              Nov 16 '18 at 15:29











            • its always result to null on NavigationController part. not i use the dependency on my Page constructor

              – Secret Coder
              Nov 16 '18 at 15:30













            • How is your Navigation Starts ?

              – Nirmal Subedi
              Nov 16 '18 at 15:33











            • it started from a master detail page

              – Secret Coder
              Nov 16 '18 at 15:45
















            0














            I think this should work:



            var navheight = GetTopViewController().NavigationController.NavigationBar.Frame.Height;


            public static UIViewController GetTopViewController()
            {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;

            if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();

            return vc;
            }





            share|improve this answer


























            • i already tried that and it crash my app or it resullts to null

              – Secret Coder
              Nov 16 '18 at 15:28













            • What error you got?

              – Nirmal Subedi
              Nov 16 '18 at 15:29











            • its always result to null on NavigationController part. not i use the dependency on my Page constructor

              – Secret Coder
              Nov 16 '18 at 15:30













            • How is your Navigation Starts ?

              – Nirmal Subedi
              Nov 16 '18 at 15:33











            • it started from a master detail page

              – Secret Coder
              Nov 16 '18 at 15:45














            0












            0








            0







            I think this should work:



            var navheight = GetTopViewController().NavigationController.NavigationBar.Frame.Height;


            public static UIViewController GetTopViewController()
            {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;

            if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();

            return vc;
            }





            share|improve this answer















            I think this should work:



            var navheight = GetTopViewController().NavigationController.NavigationBar.Frame.Height;


            public static UIViewController GetTopViewController()
            {
            var window = UIApplication.SharedApplication.KeyWindow;
            var vc = window.RootViewController;
            while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;

            if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();

            return vc;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 15:54

























            answered Nov 16 '18 at 15:28









            Nirmal SubediNirmal Subedi

            1,5002924




            1,5002924













            • i already tried that and it crash my app or it resullts to null

              – Secret Coder
              Nov 16 '18 at 15:28













            • What error you got?

              – Nirmal Subedi
              Nov 16 '18 at 15:29











            • its always result to null on NavigationController part. not i use the dependency on my Page constructor

              – Secret Coder
              Nov 16 '18 at 15:30













            • How is your Navigation Starts ?

              – Nirmal Subedi
              Nov 16 '18 at 15:33











            • it started from a master detail page

              – Secret Coder
              Nov 16 '18 at 15:45



















            • i already tried that and it crash my app or it resullts to null

              – Secret Coder
              Nov 16 '18 at 15:28













            • What error you got?

              – Nirmal Subedi
              Nov 16 '18 at 15:29











            • its always result to null on NavigationController part. not i use the dependency on my Page constructor

              – Secret Coder
              Nov 16 '18 at 15:30













            • How is your Navigation Starts ?

              – Nirmal Subedi
              Nov 16 '18 at 15:33











            • it started from a master detail page

              – Secret Coder
              Nov 16 '18 at 15:45

















            i already tried that and it crash my app or it resullts to null

            – Secret Coder
            Nov 16 '18 at 15:28







            i already tried that and it crash my app or it resullts to null

            – Secret Coder
            Nov 16 '18 at 15:28















            What error you got?

            – Nirmal Subedi
            Nov 16 '18 at 15:29





            What error you got?

            – Nirmal Subedi
            Nov 16 '18 at 15:29













            its always result to null on NavigationController part. not i use the dependency on my Page constructor

            – Secret Coder
            Nov 16 '18 at 15:30







            its always result to null on NavigationController part. not i use the dependency on my Page constructor

            – Secret Coder
            Nov 16 '18 at 15:30















            How is your Navigation Starts ?

            – Nirmal Subedi
            Nov 16 '18 at 15:33





            How is your Navigation Starts ?

            – Nirmal Subedi
            Nov 16 '18 at 15:33













            it started from a master detail page

            – Secret Coder
            Nov 16 '18 at 15:45





            it started from a master detail page

            – Secret Coder
            Nov 16 '18 at 15:45













            0














            Solution:



            How about pass an instance of viewController as parameter in the function inside the IDeviceInfo?



            Try this:



             public void getNaviHeight(ContentPage vc)
            {
            var renderer = Platform.GetRenderer(vc);
            if (renderer == null)
            {
            renderer = RendererFactory.GetRenderer(vc);
            Platform.SetRenderer(vc, renderer);
            }
            var viewController = renderer.ViewController;

            var h = viewController.NavigationController?.NavigationBar.Frame.Height;

            }


            And use the dependency:



            public MainPage ()
            {
            DependencyService.Get<IDeviceInfo>().getNaviHeight(this);
            }





            share|improve this answer
























            • not working. NavigatioController always null

              – Secret Coder
              Nov 21 '18 at 17:47











            • Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:09











            • I get a ViewController but the NavigationController is always null

              – Secret Coder
              Nov 22 '18 at 3:13











            • So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:17











            • Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

              – Secret Coder
              Nov 22 '18 at 6:12
















            0














            Solution:



            How about pass an instance of viewController as parameter in the function inside the IDeviceInfo?



            Try this:



             public void getNaviHeight(ContentPage vc)
            {
            var renderer = Platform.GetRenderer(vc);
            if (renderer == null)
            {
            renderer = RendererFactory.GetRenderer(vc);
            Platform.SetRenderer(vc, renderer);
            }
            var viewController = renderer.ViewController;

            var h = viewController.NavigationController?.NavigationBar.Frame.Height;

            }


            And use the dependency:



            public MainPage ()
            {
            DependencyService.Get<IDeviceInfo>().getNaviHeight(this);
            }





            share|improve this answer
























            • not working. NavigatioController always null

              – Secret Coder
              Nov 21 '18 at 17:47











            • Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:09











            • I get a ViewController but the NavigationController is always null

              – Secret Coder
              Nov 22 '18 at 3:13











            • So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:17











            • Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

              – Secret Coder
              Nov 22 '18 at 6:12














            0












            0








            0







            Solution:



            How about pass an instance of viewController as parameter in the function inside the IDeviceInfo?



            Try this:



             public void getNaviHeight(ContentPage vc)
            {
            var renderer = Platform.GetRenderer(vc);
            if (renderer == null)
            {
            renderer = RendererFactory.GetRenderer(vc);
            Platform.SetRenderer(vc, renderer);
            }
            var viewController = renderer.ViewController;

            var h = viewController.NavigationController?.NavigationBar.Frame.Height;

            }


            And use the dependency:



            public MainPage ()
            {
            DependencyService.Get<IDeviceInfo>().getNaviHeight(this);
            }





            share|improve this answer













            Solution:



            How about pass an instance of viewController as parameter in the function inside the IDeviceInfo?



            Try this:



             public void getNaviHeight(ContentPage vc)
            {
            var renderer = Platform.GetRenderer(vc);
            if (renderer == null)
            {
            renderer = RendererFactory.GetRenderer(vc);
            Platform.SetRenderer(vc, renderer);
            }
            var viewController = renderer.ViewController;

            var h = viewController.NavigationController?.NavigationBar.Frame.Height;

            }


            And use the dependency:



            public MainPage ()
            {
            DependencyService.Get<IDeviceInfo>().getNaviHeight(this);
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 6:19









            Jack Hua - MSFTJack Hua - MSFT

            1,449129




            1,449129













            • not working. NavigatioController always null

              – Secret Coder
              Nov 21 '18 at 17:47











            • Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:09











            • I get a ViewController but the NavigationController is always null

              – Secret Coder
              Nov 22 '18 at 3:13











            • So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:17











            • Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

              – Secret Coder
              Nov 22 '18 at 6:12



















            • not working. NavigatioController always null

              – Secret Coder
              Nov 21 '18 at 17:47











            • Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:09











            • I get a ViewController but the NavigationController is always null

              – Secret Coder
              Nov 22 '18 at 3:13











            • So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

              – Jack Hua - MSFT
              Nov 22 '18 at 3:17











            • Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

              – Secret Coder
              Nov 22 '18 at 6:12

















            not working. NavigatioController always null

            – Secret Coder
            Nov 21 '18 at 17:47





            not working. NavigatioController always null

            – Secret Coder
            Nov 21 '18 at 17:47













            Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

            – Jack Hua - MSFT
            Nov 22 '18 at 3:09





            Did you get the viewController? Can you provide a simple demo or some more codes to show the structure of your project?

            – Jack Hua - MSFT
            Nov 22 '18 at 3:09













            I get a ViewController but the NavigationController is always null

            – Secret Coder
            Nov 22 '18 at 3:13





            I get a ViewController but the NavigationController is always null

            – Secret Coder
            Nov 22 '18 at 3:13













            So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

            – Jack Hua - MSFT
            Nov 22 '18 at 3:17





            So, is the viewController you get same as the controller your want? And Is the viewController really have a NavigationController in your code?

            – Jack Hua - MSFT
            Nov 22 '18 at 3:17













            Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

            – Secret Coder
            Nov 22 '18 at 6:12





            Im not sure with the viewcontroller I get is what I want. The page that I get has navigation bar with menu icon/burger icon. So im sure there should be a NavigationController

            – Secret Coder
            Nov 22 '18 at 6:12











            0














            this worked to me:



            var navigationBar = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Subviews[0].Subviews.OfType<UINavigationBar>().FirstOrDefault();

            if(navigationBar != null)
            {
            // continue here...
            }





            share|improve this answer




























              0














              this worked to me:



              var navigationBar = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Subviews[0].Subviews.OfType<UINavigationBar>().FirstOrDefault();

              if(navigationBar != null)
              {
              // continue here...
              }





              share|improve this answer


























                0












                0








                0







                this worked to me:



                var navigationBar = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Subviews[0].Subviews.OfType<UINavigationBar>().FirstOrDefault();

                if(navigationBar != null)
                {
                // continue here...
                }





                share|improve this answer













                this worked to me:



                var navigationBar = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Subviews[0].Subviews.OfType<UINavigationBar>().FirstOrDefault();

                if(navigationBar != null)
                {
                // continue here...
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 5 at 13:15









                Rudolf StepanRudolf Stepan

                114




                114






























                    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%2f53340354%2fgetting-navigation-bar-height-in-dependency-service-xamarin-forms%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma