我的应用中有两个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
,但它没有给我任何结果。有什么想法吗?
答案 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
不在屏幕上,因此您可以更改导航栏。