更改TabBar颜色

时间:2016-04-25 12:55:09

标签: xamarin xamarin.ios

我试图在iOS中更改UITabBarItem颜色。我正在使用MVVMCross和Stuart的简单示例:

var viewControllers = new UIViewController[]
{
    CreateTabFor("1", "home", FirstViewModel.Child1),
    CreateTabFor("2", "twitter", FirstViewModel.Child2),
    CreateTabFor("3", "favorites", FirstViewModel.Child3),
};
ViewControllers = viewControllers;
CustomizableViewControllers = new UIViewController[] { };
SelectedViewController = ViewControllers[0];


private int _createdSoFarCount = 0;

private UIViewController CreateTabFor(string title, string imageName, IMvxViewModel viewModel)
{
    var controller = new UINavigationController();
    var screen = this.CreateViewControllerFor(viewModel) as UIViewController;
    SetTitleAndTabBarItem(screen, title, imageName);
    controller.PushViewController(screen, false);
    return controller;
}

private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName)
{
    screen.Title = title;
    screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle("Images/Tabs/" + imageName + ".png"),
                                             _createdSoFarCount);
    _createdSoFarCount++;
}

有示例图片: enter image description here

1 个答案:

答案 0 :(得分:1)

按颜色,您的意思是更改图像吗?你可以这样做:

private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName)
    {
        screen.Title = title;

        var offImage = UIImage.FromBundle (imageName);
        offImage = offImage.ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal);
        offImage.AccessibilityLabel = title;

        var onImage = UIImage.FromBundle (imageName + "selected");
        onImage = onImage.ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal);
        onImage.AccessibilityLabel = title;

        var tabBarItem = new UITabBarItem (title, offImage, onImage);
        tabBarItem.AccessibilityLabel = title;

        screen.TabBarItem = tabBarItem;

        _createdSoFarCount++;
    }

此构造函数有一个selectedImage和一个image参数来更改选定的状态https://developer.xamarin.com/api/constructor/UIKit.UITabBarItem.UITabBarItem/p/System.String/UIKit.UIImage/UIKit.UIImage/

var tabBarItem = new UITabBarItem (title, offImage, onImage);

<强>更新

SelectedBackGroundTint 您可以使用以下方式设置所有UITabBar的外观:

UITabBar.Appearance.SelectedImageTintColor = UIColor.Red;

TextColor 您可以使用以下方式设置所有UITabBarItem的外观:

UITabBarItem.Appearance.SetTitleTextAttributes (new UITextAttributes () {
        TextColor = UIColor.Red
}, UIControlState.Normal);

UITabBarItem.Appearance.SetTitleTextAttributes (new UITextAttributes () {
        TextColor = UIColor.Blue
}, UIControlState.Selected);

BackGroundColor 您可以使用以下方式设置所有UITabBar的外观:

UITabBar.Appearance.TintColor = UIColor.Magenta;