Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[iOS] Implemented SelectedTintColor and UnselectedTintColor for iOS #3519

Closed
wants to merge 7 commits into from
Closed
Expand Up @@ -43,6 +43,14 @@ ContentPage CreatePage(ICommand restore, string title)
{
Text = "Toggle PreferredStatusBarUpdateAnimation"
};
var setSelectedItemColorToRedButton = new Button
{
Text = "Set selected item color to red"
};
var setUnselectedItemColorToRedButton = new Button
{
Text = "Set unselected item color to yellow"
};

togglePrefersStatusBarHiddenButton.Command = new Command(() =>
{
Expand Down Expand Up @@ -77,12 +85,24 @@ ContentPage CreatePage(ICommand restore, string title)
page.On<iOS>().SetPreferredStatusBarUpdateAnimation(UIStatusBarAnimation.Fade);
});

setSelectedItemColorToRedButton.Command = new Command(() =>
{
On<iOS>().SetSelectedTintColor(Color.Red);
});

setUnselectedItemColorToRedButton.Command = new Command(() =>
{
On<iOS>().SetUnselectedTintColor(Color.Yellow);
});

var restoreButton = new Button { Text = "Back To Gallery" };
restoreButton.Clicked += (sender, args) => restore.Execute(null);
content.Children.Add(restoreButton);
content.Children.Add(togglePrefersStatusBarHiddenButton);
content.Children.Add(togglePrefersStatusBarHiddenForPageButton);
content.Children.Add(togglePreferredStatusBarUpdateAnimationButton);
content.Children.Add(setSelectedItemColorToRedButton);
content.Children.Add(setUnselectedItemColorToRedButton);
content.Children.Add(new Label
{
HorizontalOptions = LayoutOptions.Center,
Expand Down
@@ -0,0 +1,57 @@
namespace Xamarin.Forms.PlatformConfiguration.iOSSpecific
{
using FormsElement = Forms.TabbedPage;

public static class TabbedPage
{
public static readonly BindableProperty UnselectedTintColorProperty =
BindableProperty.Create("UnselectedTintColor", typeof(Color),
typeof(TabbedPage), Color.Default);

public static Color GetUnselectedTintColor(BindableObject element)
{
return (Color)element.GetValue(UnselectedTintColorProperty);
}

public static void SetUnselectedTintColor(BindableObject element, Color value)
{
element.SetValue(UnselectedTintColorProperty, value);
}

public static IPlatformElementConfiguration<iOS, FormsElement> SetUnselectedTintColor(this IPlatformElementConfiguration<iOS, FormsElement> config, Color value)
{
SetUnselectedTintColor(config.Element, value);
return config;
}

public static Color GetUnselectedTintColor(this IPlatformElementConfiguration<iOS, FormsElement> config)
{
return GetUnselectedTintColor(config.Element);
}

public static readonly BindableProperty SelectedTintColorProperty =
BindableProperty.Create("SelectedTintColor", typeof(Color),
typeof(TabbedPage), Color.Default);

public static Color GetSelectedTintColor(BindableObject element)
{
return (Color)element.GetValue(SelectedTintColorProperty);
}

public static void SetSelectedTintColor(BindableObject element, Color value)
{
element.SetValue(SelectedTintColorProperty, value);
}

public static IPlatformElementConfiguration<iOS, FormsElement> SetSelectedTintColor(this IPlatformElementConfiguration<iOS, FormsElement> config, Color value)
{
SetSelectedTintColor(config.Element, value);
return config;
}

public static Color GetSelectedTintColor(this IPlatformElementConfiguration<iOS, FormsElement> config)
{
return GetSelectedTintColor(config.Element);
}
}
}
21 changes: 21 additions & 0 deletions Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using UIKit;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using static Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page;
using PageUIStatusBarAnimation = Xamarin.Forms.PlatformConfiguration.iOSSpecific.UIStatusBarAnimation;

Expand Down Expand Up @@ -52,6 +53,9 @@ public UIView NativeView
get { return View; }
}

public Color SelectedTintColor => (Element != null) ? Tabbed.OnThisPlatform().GetSelectedTintColor() : Color.Default;
public Color UnselectedTintColor => (Element != null) ? Tabbed.OnThisPlatform().GetUnselectedTintColor() : Color.Default;

public void SetElement(VisualElement element)
{
var oldElement = Element;
Expand All @@ -73,6 +77,7 @@ public void SetElement(VisualElement element)

UpdateBarBackgroundColor();
UpdateBarTextColor();
UpdateTintColor();

EffectUtilities.RegisterEffectControlProvider(this, oldElement, element);
}
Expand Down Expand Up @@ -210,6 +215,7 @@ void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e)

UpdateBarBackgroundColor();
UpdateBarTextColor();
UpdateTintColor();
}

void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
Expand All @@ -234,6 +240,9 @@ void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
UpdatePrefersStatusBarHiddenOnPages();
else if (e.PropertyName == PreferredStatusBarUpdateAnimationProperty.PropertyName)
UpdateCurrentPagePreferredStatusBarUpdateAnimation();
else if (e.PropertyName == PlatformConfiguration.iOSSpecific.TabbedPage.SelectedTintColorProperty.PropertyName ||
e.PropertyName == PlatformConfiguration.iOSSpecific.TabbedPage.UnselectedTintColorProperty.PropertyName)
UpdateTintColor();
}

public override UIViewController ChildViewControllerForStatusBarHidden()
Expand Down Expand Up @@ -363,6 +372,18 @@ void UpdateBarTextColor()
TabBar.TintColor = isDefaultColor ? _defaultBarTextColor : barTextColor.ToUIColor();
}

void UpdateTintColor()
{
if (Tabbed == null || TabBar == null || TabBar.Items == null)
return;

if (SelectedTintColor != Color.Default)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you want to return to the default color? you will need to save the default color of the platform and use it when the Color is set to Default. and had change before.

TabBar.SelectedImageTintColor = SelectedTintColor.ToUIColor();

if (Forms.IsiOS10OrNewer && UnselectedTintColor != Color.Default)
TabBar.UnselectedItemTintColor = UnselectedTintColor.ToUIColor();
}

void UpdateChildrenOrderIndex(UIViewController[] viewControllers)
{
for (var i = 0; i < viewControllers.Length; i++)
Expand Down