按BackButton Xamarin.iOS时,NavigationBar色调颜色不会改变

时间:2016-04-20 09:48:45

标签: xamarin uinavigationcontroller xamarin.ios viewdidappear bartintcolor

我的应用中有两个ViewControllers。我正在使用Xamarin.iOS。我想从VC1导航到VC2并更改NavigationBarBarTintColor)的背景颜色,以便不同的VC应该有不同的NavigationBarColors

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);
        }

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);
        }

现在的问题是:当我从VC1导航到VC2时,我能够改变颜色,但当我回到VC1时,颜色不会改变。

我的猜测是,当屏幕出现改变背景颜色时,我需要覆盖某些方法。在VC1中,我覆盖了ViewDidAppear,但它没有给我任何结果。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

嘿,你试过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);
    }
}

使用ViewDidAppear视图已在屏幕上且ViewWillAppear不在屏幕上,因此您可以更改导航栏。