如何在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:
- 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; } } } }
- Replace
YourAppNamewith your actual project namespace, and adjust the color values to match your design.
Key Notes
UnselectedItemTintColoris 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

