当我按下后退按钮时,导航栏色调颜色没有改变 Xamarin.iOS

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

我的应用程序中有两个 ViewControllers。我正在使用 Xamarin.iOS。我想从 VC1 导航到 VC2 并更改 NavigationBar (BarTintColor) 的背景颜色,因此不同的 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 但它没有给我任何结果。有什么想法吗?

嘿,你试过了吗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 它不在屏幕上,因此您可以更改导航栏。