You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin.iOS中设置非活跃标签栏色调颜色?

How to Set Inactive Tab Bar Tint Color in Xamarin.iOS

Hey there, I totally get the frustration—you know Xcode has a straightforward setting for this, but translating that to Xamarin.iOS feels like hitting a wall at first. The good news is we can still tap into the same underlying UIKit APIs that Xcode uses, just wrapped in C#. Let’s walk through the solutions for both native Xamarin.iOS projects and Xamarin.Forms setups.

Native Xamarin.iOS Project Solution

Starting with the simplest case: if you’re working directly with UITabBarController, you can use the UnselectedItemTintColor property (available in iOS 10+). This handles both icon and text color for inactive tabs in one go.

Add this code to your UITabBarController’s ViewDidLoad method:

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    
    // Check iOS version first to avoid runtime errors
    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
        // Set your desired inactive tint color here
        TabBar.UnselectedItemTintColor = UIColor.Gray;
        // Optional: Set active tab tint color if needed
        TabBar.TintColor = UIColor.SystemBlueColor;
    }
    else
    {
        // For iOS 9 and below, we have to handle icons and text separately
        foreach (var tabItem in TabBar.Items)
        {
            // Configure inactive icon color
            if (tabItem.Image != null)
            {
                // Keep original icon colors for inactive state
                tabItem.Image = tabItem.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
            }
            
            // Configure inactive text color
            var unselectedTextAttrs = new UITextAttributes
            {
                TextColor = UIColor.LightGray
            };
            tabItem.SetTitleTextAttributes(unselectedTextAttrs, UIControlState.Normal);
            
            // Configure active text color
            var selectedTextAttrs = new UITextAttributes
            {
                TextColor = UIColor.SystemBlueColor
            };
            tabItem.SetTitleTextAttributes(selectedTextAttrs, UIControlState.Selected);
        }
    }
}

Xamarin.Forms TabbedPage Solution

If you’re using Xamarin.Forms’ TabbedPage, you’ll need a custom renderer to access the native UITabBar control:

  1. Create a custom renderer class in your iOS project:
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace YourAppName.iOS.Renderers
{
    public class CustomTabbedPageRenderer : TabbedRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            
            if (TabBar != null && UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // Set inactive tab tint color
                TabBar.UnselectedItemTintColor = UIColor.FromRGB(120, 120, 120);
                // Set active tab tint color
                TabBar.TintColor = UIColor.SystemGreenColor;
            }
        }
    }
}
  1. Replace YourAppName with your actual project namespace, and adjust the color values to match your design.

Key Notes

  • UnselectedItemTintColor is a huge time-saver for iOS 10+—it automatically applies to both icons and text.
  • For older iOS versions, you have to handle icons and text separately because the unified property didn’t exist yet.
  • Always check the iOS version before using newer APIs to prevent crashes on older devices.

That should replicate the Xcode behavior you’re used to. Let me know if you run into any weird edge cases with specific iOS versions!

内容的提问来源于stack exchange,提问作者user7885142

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:19:33