1

Using Xamarin.Forms, I have a custom TabbedPageRenderer in iOS. Now, I can change the text color on a selected TabBarItem, but I can't change the background color of the selected tab. Does anyone know how?

class CustomTabbedPageRenderer : TabbedRenderer
{
   public override UIViewController SelectedViewController
   {
       get
       {
           UITextAttributes attr = new UITextAttributes();
           attr.TextColor = UIColor.White;
           if (base.SelectedViewController != null)
           {
               base.SelectedViewController.TabBarItem.SetTitleTextAttributes(attr, UIControlState.Normal);                  
               // TODO: How to set background color for ONE item?
           }
           return base.SelectedViewController;
       }
       set
       {
           base.SelectedViewController = value;
       }
    }
}
Mystogan
  • 547
  • 4
  • 21

2 Answers2

6

The Best Solution:

Set Appearance in method ViewWillAppear in TabbedRenderer

Code:

[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace TabbedPageWithNavigationPage.iOS
{
    class CustomTabbedPageRenderer : TabbedRenderer
    {
        public UIImage imageWithColor(CGSize size)
        {
            CGRect rect = new CGRect(0, 0, size.Width, size.Height);
            UIGraphics.BeginImageContext(size);

            using (CGContext context = UIGraphics.GetCurrentContext())
            {
                context.SetFillColor(UIColor.Red.CGColor);
                context.FillRect(rect);
            }

            UIImage image = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            return image;
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            CGSize size = new CGSize(TabBar.Frame.Width / TabBar.Items.Length, TabBar.Frame.Height);

            //Background Color
            UITabBar.Appearance.SelectionIndicatorImage = imageWithColor(size);
            //Normal title Color
            UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
            //Selected title Color
            UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.Black }, UIControlState.Selected);
        }
    }
}

enter image description here

Community
  • 1
  • 1
ColeX
  • 14,062
  • 5
  • 43
  • 240
2

You can use appearance API to change background color on tabbed page

OR

you can use custom render(as you tried here)

[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageCustom))]

namespace MobileCRM.iOS {     


public class TabbedPageCustom : TabbedRenderer  {    

public TabbedPageCustom ()   {      

   TabBar.TintColor = MonoTouch.UIKit.UIColor.Black;
   TabBar.BarTintColor = MonoTouch.UIKit.UIColor.Blue;  
   TabBar.BackgroundColor = MonoTouch.UIKit.UIColor.Green;         
}    

}

}

Hope you can continue from this...