I have a Xamarin Forms
application and I am currently working on code for an iOS. In my settings I have an option to change the application's theme (Dark and Light). This basically just changes the background colors and text colors of the pages. Now what I want to do is to be able to change the SelectedImageTintColor
and BarTintColor
of the TabBar
as well as the the BarTintColor
and TintColor
of the NavigationBar
. At the moment I have create a renderer for the tabbed page:
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
App.theme = (Theme)App.DB.GetIntSetting("ThemeColor");
switch (App.theme)
{
case Theme.Dark:
{
TabBar.SelectedImageTintColor = UIColor.FromRGB(255, 165, 0);
TabBar.BarTintColor = UIColor.Black;
break;
}
case Theme.Light:
{
TabBar.SelectedImageTintColor = UIColor.FromRGB(60, 132, 60);
TabBar.BarTintColor = UIColor.White;
break;
}
}
}
Right now these colors would only take effect when you first start the application.
I researched this problem but could not find any answer from anyone on how to solve this problem.
I know there have been many changes to Xamarin so I would like to find out if there are any recent developments or new ways to tackle this problem. I am open to looking into any possible suggestions as part of the requirement for the application is to be able to change these colors.
Edits:
My Tabbed
page was created like the following:
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
var phrasesPage = new NavigationPage(new PhrasesPage())
{
Title = "Play",
Icon = "play1.png"
};
var settingsPage = new NavigationPage(new SettingsPage())
{
Title = "Settings",
Icon = "settings.png"
};
// other declarations here
Children.Add(phrasesPage);
Children.Add(settingsPage);
// and more
}
}
For instance, if I choose Dark theme then the TabBar
and NavigationBar
background color would be black, the TabBar
's selectedimage would be orange and the NavigationBar
's text would be white. Likewise if I choose Light theme then the TabBar
and NavigationBar
background color would be white, the TabBar
's selectedimage would be green and the NavigationBar
's text would be black.