iOS NavigationPage.SetHasNavigationBar(this, false) not working correctly

MarshallMarshall USMember ✭✭

I am using a NavigationPage to proceed from page to page in my application, and I have run into an interesting bug. In my login screen, I am calling NavigationPage.SetHasNavigationBar(this, false) in my overriden OnAppearing() method, and the login screen has no navigation bar, which is what I am looking for, and when I go to the next page, I call NavigationPage.SetHasNavigationBar(this, true) in my overriden OnDisappearing() method, and the navigation bar is now visible for all pages after. Now, when I hit the back button, and go back to the login screen, the navigation bar remains, and doesn't disappear like I want it to, and I am calling SetHasNavigationBar in my OnAppearing() method for the login screen, so shouldn't the navigation bar disappear when I return to it?

Posts

  • MarshallMarshall USMember ✭✭

    Oh yea, forgot to add, this works as expected on Android, but on iOS is where I am having this bug.

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai
    edited June 2014

    NavigationPage.SetHasNavigationBar is a per-page setting. So if your app has 2 pages you should set it once per page.

    NavigationPage.SetHasNavigationBar (firstPage, false);
    NavigationPage.SetHasNavigationBar (firstPage, true); // this is the default so is not really needed
    
  • MarshallMarshall USMember ✭✭

    so i I were to call SetHasNavigationBar(this, false) in the constructor of my login screen then it would keep the navigationbar hidden on the loginscreen, but won't affect the pages after?

  • MarshallMarshall USMember ✭✭

    ok so I moved the SetHasNavigationBar(this, false) in the constructor of my login screen, and when navigating to the next page, the navigation bar is visible, but when returning to the login screen the navigation bar still remains visible.

  • CristinaBirkel.2642CristinaBirkel.2642 USMember
    edited June 2014

    I feel like there could easily be some bug here. I am also struggling with using a NavigationPage in this way, and here's a quick sandbox example I came up with:

    namespace FormsSandbox
    {
        public class App
        {
            public static Page GetMainPage ()
            {   
                var mainPage = new TabbedPage ();
    
                var navPage1 = new NavigationPage (new RecursiveContentPage (0)){ Title = "Title 1" };
                NavigationPage.SetHasNavigationBar (navPage1, false);
    
                mainPage.Children.Add (navPage1);
                mainPage.Children.Add (new NavigationPage(new ContentPage ()) { Title = "Title 2" });
    
                return mainPage;
            }
        }
    
        public class RecursiveContentPage : ContentPage
        {
            private int page;
    
            public RecursiveContentPage(int page)
            {
                this.page = page;
    
                if(page == 0)
                    NavigationPage.SetHasNavigationBar (this, false);
    
                this.Padding = new Thickness (20, Device.OnPlatform (40, 20, 20), 20, 20);
    
                var label = new Label {
                    Text = "Page number " + page
                };
                var button = new Button {
                    Text = "Click me to go to another page.",
                };
                button.Clicked += (object sender, EventArgs e) => Navigation.PushAsync(new RecursiveContentPage(page+1));
    
                this.Content = new StackLayout {
                    Children = {label, button}
                };
            }
    
            protected override void OnAppearing()
            {
                base.OnAppearing ();
                if(page == 0)
                    NavigationPage.SetHasNavigationBar (this, false);
            }
    
            protected override void OnDisappearing()
            {
                base.OnDisppearing ();
                if(page == 1)
                    NavigationPage.SetHasNavigationBar (this, false);
            }
        }
    }
    

    Now, this code works as expected on iOS. A main page appears with a TabbedPage, and the first tab contains a RecursiveContentPage, which increments the page counter every time the button is clicked and pushes a new RecursiveContentPage onto the navigation stack. The back button appears only when page > 0.

    I want to point out, however, what didn't work.

    The initial call to NavigationPage.SetHasNavigationBar (navPage1, false); did absolutely nothing. A blank navigation bar appeared anyway when page = 0.

    The call to NavigationPage.SetHasNavigationBar (this, false); in the constructor worked initially, and the first page had no navigation bar, whereas the following pages (page > 0) had Back buttons. However, when all the Back buttons were clicked and the initial page was shown again, the blank nav bar remained.

    The next thing I tried was to call NavigationPage.SetHasNavigationBar (this, false); yet again in the overridden OnAppearing method. This did nothing.

    Finally, I added the override for the OnDisappearing method, which worked. (I don't like that this worked, actually. Maybe OnDisappearing is meant as "disappearing from the navigation stack" as opposed to "disappearing from sight?")

    Can someone please comment on this behavior, and how it's supposed to work? Could be that I'm using the whole thing wrong. Thanks!!

    Cristina

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    @CristinaBirkel.2642‌ what version of Xamarin.Forms are you using ?

  • CristinaBirkel.2642CristinaBirkel.2642 USMember
    edited June 2014

    @StephaneDelcroix‌

    Ah, the Xamarin Studio -> About page doesn't have the version info, but I found the folder on my machine containing the package. It's named: Xamarin.Forms.1.0.6186. I assume that's the version number? Thanks!

  • rmarinhormarinho PTMember, Insider, Beta Xamurai
    edited June 2014

    try update your packages @CristinaBirkel.2642‌

    in your solution, look at the packages folder, right click , update.

  • Okay I've updated to version 1.1. I still see the same behavior that I detailed above.

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    @CristinaBirkel‌:

    The initial call to NavigationPage.SetHasNavigationBar (navPage1, false); did absolutely nothing.

    You're flagging the NavigationPage, where you should flag the content of it.

    If your intent is to hide the navigation bar on the first page only, here's code that work:

    namespace FormsSandbox
    {
        public class App
        {
            public static Page GetMainPage ()
            {  
                var mainPage = new TabbedPage ();
    
                var navPage1 = new NavigationPage (new RecursiveContentPage (0)){ Title = "Title 1" };
    
                mainPage.Children.Add (navPage1);
                mainPage.Children.Add (new NavigationPage(new ContentPage ()) { Title = "Title 2" });
    
                return mainPage;
            }
        }
    
        public class RecursiveContentPage : ContentPage
        {
            private int page;
    
            public RecursiveContentPage(int page)
            {
                this.page = page;
    
                if(page == 0)
                    NavigationPage.SetHasNavigationBar (this, false);
    
                this.Padding = new Thickness (20, Device.OnPlatform (40, 20, 20), 20, 20);
    
                var label = new Label {
                    Text = "Page number " + page
                };
                var button = new Button {
                    Text = "Click me to go to another page.",
                };
                button.Clicked += (object sender, EventArgs e) => Navigation.PushAsync(new RecursiveContentPage(page+1));
    
                this.Content = new StackLayout {
                    Children = {label, button}
                };
            }
        }
    }
    
  • @StephaneDelcroix‌ thanks for taking the time to help me on this. :) However, that example does not work. When you click the < Back button and go to the original page, it has space for the navigation bar, which is the original problem I have been trying to solve. Thanks for pointing out my mistake in setting navPage1 though.

  • MarshallMarshall USMember ✭✭

    @CristinaBirkel‌ I think it's just a bug with iOS, because this works perfectly fine on Android.

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    @CristinaBirkel‌.2642 : works for me: http://screencast.com/t/Xc1ao5lA2oj

    you're probably not using the latest Xamarin.Forms: 1.1.0.6201

  • MarshallMarshall USMember ✭✭

    @StephaneDelcroix‌ what is your code for doing that? because I updated to 1.1.0.6201, and it still doesn't properly work for me

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    I'm using the code I pasted here. I'm not technically on 6201, but the fix for that should be in. if it's not, well, it'll be in the next drop.

  • Yeah I am updated as well.. maybe it is in the next update then, which would be great!

  • DanielDrysdaleDanielDrysdale AUMember

    I was having this issue on Xamarin.Forms 1.0.6186 but it works properly on 1.1.0.6201

  • DhirendraKumarDhirendraKumar INMember ✭✭
    edited July 2014

    I used the same code
    At first launch the navigation bar does not appear
    When you navigate away and come back the navigation bar appears for a second and then disappears

    `namespace HelloXamarinForms
    {
    public class App
    {
    public static Page GetMainPage ()
    {
    var mainPage = new TabbedPage ();

            var navPage1 = new NavigationPage (new RecursiveContentPage (0)){ Title = "Title 1" };
    
            mainPage.Children.Add (navPage1);
            mainPage.Children.Add (new NavigationPage(new ContentPage ()) { Title = "Title 2" });
    
            return mainPage;
        }
    }
    
    public class RecursiveContentPage : ContentPage
    {
        private int page;
    
        public RecursiveContentPage(int page)
        {
            this.page = page;
    
            if(page == 0)
                NavigationPage.SetHasNavigationBar (this, false);
    
            this.Padding = new Thickness (20, Device.OnPlatform (40, 20, 20), 20, 20);
    
            var label = new Label {
                Text = "Page number " + page
            };
            var button = new Button {
                Text = "Click me to go to another page.",
            };
            button.Clicked += (object sender, EventArgs e) => Navigation.PushAsync(new RecursiveContentPage(page+1));
    
            this.Content = new StackLayout {
                Children = {label, button}
            };
        }
    
    
        protected override void OnAppearing()
        {
            base.OnAppearing ();
            if(page == 0)
                NavigationPage.SetHasNavigationBar (this, false);
        }
    
        protected override void OnDisappearing()
        {
            base.OnDisappearing ();
            if(page == 1)
                NavigationPage.SetHasNavigationBar (this, false);
        }
    
    
    }
    }
    

    `

    I am using

    Xamarin Forms version : 1.2.1.6229
    Other
    === Xamarin Studio ===

    Version 5.1 (build 479)
    Installation UUID: XXXXXXXXXX
    Runtime:
    Mono 3.4.0 ((no/954ed3c)
    GTK+ 2.24.23 (Raleigh theme)

    Package version: 304000214
    

    === Apple Developer Tools ===

    Xcode 5.1.1 (5085)
    Build 5B1008

    === Xamarin.Android ===

    Version: 4.12.5 (Trial Edition)
    Android SDK: /Users/XXXXXXXXXXXXX/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
    2.1 (API level 7)
    2.2 (API level 8)
    2.3 (API level 10)
    3.1 (API level 12)
    4.0 (API level 14)
    4.0.3 (API level 15)
    4.1 (API level 16)
    4.2 (API level 17)
    4.3 (API level 18)
    4.4 (API level 19)
    Java SDK: /usr
    java version "1.6.0_65"
    Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
    Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

    === Xamarin.Mac ===

    Version:

    === Xamarin.iOS ===

    Version: 7.2.5.4 (Trial Edition)
    Hash: e7f4f92
    Branch:
    Build date: 2014-06-18 02:37:27-0400

    === Build Information ===

    Release ID: 501000479
    Git revision: e5a428cec75d4cc7e6e3ccd8192a3660d013e7dc
    Build date: 2014-06-26 09:28:17-04
    Xamarin addins: 190d93e026e17280e75a3680ef38f2630b9228d3

    === Operating System ===

    Mac OS X 10.9.3
    Darwin XXXXXXXXXXX 13.2.0 Darwin Kernel Version 13.2.0
    Thu Apr 17 23:03:13 PDT 2014
    root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64

Sign In or Register to comment.