xamarinuinavigationcontrollerxamarin.iosviewdidappearbartintcolor

The NavigationBar Tint Color doesn't change when I press BackButton Xamarin.iOS


I have two ViewControllers in my app. I am using Xamarin.iOS. I want to navigate from VC1 to VC2 and change the background color of NavigationBar (BarTintColor) so different VCs should have different NavigationBarColors.

Code in VC1:

public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            //change the navigation bar controller
            this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (26f / 255f, 56f / 255f, 100f / 255f);
            this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes()
            {
                ForegroundColor = UIColor.White
            };
            NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
        }

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);

            this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (26f / 255f, 56f / 255f, 100f / 255f);
        }

Code in VC2:

public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // Perform any additional setup after loading the view, typically from a nib.

            // change the back button
            UIBarButtonItem newBackButton = new UIBarButtonItem("Prapa",UIBarButtonItemStyle.Plain, (sender,args) => NavigationController.PopViewController (true));
            newBackButton.TintColor = UIColor.White;
            NavigationItem.SetLeftBarButtonItem (newBackButton, false);

            //change the title of the screen
            NavigationItem.Title = "Horoskopi Ditor";

            NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB(47f/255f,189f/255f, 184f/255f);
        }

Now the problem is: I am able to change the color when I navigate form VC1 to VC2 but when I go back to VC1 the color doesn't change.

My guess is that I need to override some method than, when a screen appears to change the background color. In the VC1 I have override the ViewDidAppear but it gave me no result. Any Idea?


Solution

  • Hey did you try ViewWillAppear:

    VC1:

    public override void ViewWillAppear (bool animated)
    {
        base.ViewWillAppear (animated);
        if (NavigationController != null)
        {
            NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (26f / 255f, 56f / 255f, 100f / 255f);
        }
    }
    

    With ViewDidAppear the view is already on Screen and ViewWillAppear its not on screen so you can change the Navbar.