0
votes

Since I've updated to Xamarin.Forms 4.6 my NavigationBar colors are overwritten by any source that I can't figure out.

I'm setting the BarTintColor accordingly to the current Theme (Dark/Light) like this:

this.NavigationController.NavigationBar.BarTintColor = Color.FromHex("#212121").ToUIColor();

But it keeps getting overwritten by either Solid Black or Gray (depending on Dark/Light). I also tried setting it via UINavigationBar.Appearance.BarTintColor, no change either. Additionally I'm setting the TintColor (Font Color of the bar) like this:

this.NavigationBar.TintColor = UIColor.FromRGB(38, 100, 137);

which works fine when I start the app, but as soon as I navigate somewhere else in my application it changes back to default system blue.

1
Are you sure that it has appeared SINCE 4.6? Also, why don't you set it inside the Shared project, since you are using Forms?Mihail Duchev

1 Answers

2
votes

In Xamarin Forms, there is a direct way to modify the color of NavigationBar in App.xaml.cs.

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage())
    {
        BackgroundColor = Color.Yellow,
        BarTextColor = Color.Black
    };
}

The effect:

enter image description here

If you need to modify color of status bar, you can write code in AppDelegate.cs as follows:

public override void OnActivated(UIApplication uiApplication)
{
    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        // If VS has updated to the latest version , you can use StatusBarManager , else use the first line code
        // UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
        UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
        statusBar.BackgroundColor = UIColor.White;
        UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
    }
    else
    {
        UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
        if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
        {
            statusBar.BackgroundColor = UIColor.White;
            UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
        }
    }
    base.OnActivated(uiApplication);
}

The effect :

enter image description here