我的应用程序中有两个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
不显示在屏幕中,因此您可以更改导航栏。