#Xamarin Forms Shell – How to make it work on iOS 9

images.jpg Xamarin Forms Shell is a great way to reduce your code complexity. It handles a lot of plumbing for you. Highly recommended; I use it in my 50,000 user app.

However, it doesn’t work on iOS 9 as of version 4.1. This article will show you how to get it to work on iOS 9

Credits: Based on discussions from a GitHub forum, code is taken from there so all create to the forum contributors listed there. Thanks! https://github.com/JRPMike

What’s the problem:

Tabs. iOS 9 doesn’t allow certain colorings of tabs (UnselectedItemTintColor), so we need to ensure that we don’t ask for a color to be applied when we are in iOS 9

We are going to add a ShellRenderer so that in iOS we can call the Tab colorings only when we are in iOS 10 and above.

Add a file called MyShellRenderer.cs to your iOS project only.

Then add this:

[assemblyExportRenderer(typeof(Shell), typeof(App.iOS.Renderers.MyShellRenderer))]
namespace App.iOS.Renderers
{

    public class MyShellRenderer : ShellRenderer
    {
 
        protected override void OnElementSet(Shell element)
        {
            base.OnElementSet(element);
        }
 
        protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
        {
	        return new MySafeShellTabBarAppearanceTracker();
        }
    
 
        
    }
 
	public class MySafeShellTabBarAppearanceTracker : IShellTabBarAppearanceTracker
	{
		UIColor _defaultBarTint;
		UIColor _defaultTint;
		UIColor _defaultUnselectedTint;
 
		public void ResetAppearance(UITabBarController controller)
		{
			if (_defaultTint == null)
				return;
 
			var tabBar = controller.TabBar;
			tabBar.BarTintColor = _defaultBarTint;
			tabBar.TintColor = _defaultTint;
			tabBar.UnselectedItemTintColor = _defaultUnselectedTint;
		}
 
		public void SetAppearance(UITabBarController controllerShellAppearance appearance)
		{
			IShellAppearanceElement appearanceElement = appearance;
			var backgroundColor = appearanceElement.EffectiveTabBarBackgroundColor;
			var foregroundColor = appearanceElement.EffectiveTabBarForegroundColor// currently unused
			var disabledColor = appearanceElement.EffectiveTabBarDisabledColor// unused on iOS
			var unselectedColor = appearanceElement.EffectiveTabBarUnselectedColor;
			var titleColor = appearanceElement.EffectiveTabBarTitleColor;
 
			var tabBar = controller.TabBar;
			bool operatingSystemHasUnselectedTint = UIDevice.CurrentDevice.CheckSystemVersion(10, 0);
			if (_defaultTint == null)
			{
				_defaultBarTint = tabBar.BarTintColor;
				_defaultTint = tabBar.TintColor;
				if (operatingSystemHasUnselectedTint)
				{
					_defaultUnselectedTint = tabBar.UnselectedItemTintColor;
				}
			}
 
			if (!backgroundColor.IsDefault)	tabBar.BarTintColor = backgroundColor.ToUIColor();
			if (!titleColor.IsDefault)	tabBar.TintColor = titleColor.ToUIColor();
			if (operatingSystemHasUnselectedTint)
			{
				if (!unselectedColor.IsDefault)
					tabBar.UnselectedItemTintColor = unselectedColor.ToUIColor();
			}
		}
 
		public void UpdateLayout(UITabBarController controller)
		{
		}
 
		#region IDisposable Support
 
		protected virtual void Dispose(bool disposing)
		{
		}
 
		public void Dispose()
		{
			Dispose(true);
		}
 
		#endregion
	}
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s