如何更改标签栏上的非活动图标/文本颜色?

Pab*_*blo 57 objective-c uitabbarcontroller uitabbaritem uitabbar ios

如何更改iOS 7标签栏上的非活动图标/文本颜色?灰色的那个.

在此输入图像描述

ank*_*nka 114

您还可以Render As直接在资产目录中设置标签栏图像的属性.在那里,您可以选择将属性设置为Default,Original ImageTemplate Image.

设置图像的渲染选项


Gab*_*ana 87

在每个TabBar的每个第一个ViewController中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // changing the unselected image color, you should change the selected image 
    // color if you want them to be different
    self.tabBarItem.selectedImage = [[UIImage imageNamed:@"yourImage_selectedImage"]
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.tabBarItem.image = [[UIImage imageNamed:@"yourImage_image"] 
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
Run Code Online (Sandbox Code Playgroud)

这段代码的线索是'UIImageRenderingModeAlwaysOriginal':

Apple文档的渲染模式:

UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used    
UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information
Run Code Online (Sandbox Code Playgroud)

要更改文字颜色:

在AppDelegate中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Add this if you only want to change Selected Image color 
    // and/or selected image text
    [[UITabBar appearance] setTintColor:[UIColor redColor]];

    // Add this code to change StateNormal text Color,
    [UITabBarItem.appearance setTitleTextAttributes:
    @{NSForegroundColorAttributeName : [UIColor greenColor]} 
    forState:UIControlStateNormal];

    // then if StateSelected should be different, you should add this code
    [UITabBarItem.appearance setTitleTextAttributes:
    @{NSForegroundColorAttributeName : [UIColor purpleColor]} 
    forState:UIControlStateSelected];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 在每个`viewController`中设置tabBarItem是个坏主意,因为它们不一定会被实例化.但即便如此,在iOS 7上,现有的图像也会消失.调试器显示图像已正确加载.他们有alpha透明度. (5认同)
  • 我的意思是,在每个TabBar的每个第一个ViewController中. (2认同)

Vai*_*wad 14

用于更改tabbar的取消选择图标的颜色

对于iOS 10以下:

// this code need to be placed on home page of tabbar    
for(UITabBarItem *item in self.tabBarController.tabBar.items) {
    item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
Run Code Online (Sandbox Code Playgroud)

iOS 10以上:

// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)


Ste*_*omp 10

而是将其添加到每个UIViewController,您可以创建扩展并更改UITabBarController的外观

更改未选择的图标颜色

extension UITabBarController {
    override public func viewDidLoad() {
        super.viewDidLoad()

        tabBar.items?.forEach({ (item) -> () in
           item.image = item.selectedImage?.imageWithColor(UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal)
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

更改所选图标颜色

let tabBarAppearance = UITabBar.appearance()
tabBarAppearance.tintColor = UIColor.blackColor()
Run Code Online (Sandbox Code Playgroud)

更改(取消)选定的标题颜色

let tabBarItemApperance = UITabBarItem.appearance()
tabBarItemApperance.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Edmondsans-Bold", size: 10)!, NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Normal)
tabBarItemApperance.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Edmondsans-Bold", size: 10)!, NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Selected)
Run Code Online (Sandbox Code Playgroud)

UIImage扩展

extension UIImage {
    func imageWithColor(color1: UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        color1.setFill()

        let context = UIGraphicsGetCurrentContext()
        CGContextTranslateCTM(context!, 0, self.size.height)
        CGContextScaleCTM(context!, 1.0, -1.0);
        CGContextSetBlendMode(context!, .Normal)

        let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
        CGContextClipToMask(context!, rect, self.CGImage!)
        CGContextFillRect(context!, rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
        UIGraphicsEndImageContext()

        return newImage
    }
}
Run Code Online (Sandbox Code Playgroud)


Umi*_*aya 7

通过仅使用appdelegate.m,可以更好地使用每个ViewController

在你的AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions功能中,试试这个.

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;

// repeat for every tab, but increment the index each time
UITabBarItem *firstTab = [tabBar.items objectAtIndex:0];

// also repeat for every tab
firstTab.image = [[UIImage imageNamed:@"someImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
firstTab.selectedImage = [[UIImage imageNamed:@"someImageSelected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Run Code Online (Sandbox Code Playgroud)


Ahm*_*tfy 7

要更改选项卡选择颜色而不是蓝色:

  1. 选择tabItem.
  2. 从右侧菜单中的"显示标识检查器".
  3. 使用您喜欢的颜色设置"tintColor"属性.

在此输入图像描述


Pre*_*ani 5

不要在每个 viewController 中为 tabBarItem 添加渲染图像代码,而是使用扩展

extension UITabBar{
     func inActiveTintColor() {
        if let items = items{
            for item in items{
                item.image =  item.image?.withRenderingMode(.alwaysOriginal)
                item.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.green], for: .normal)
                item.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的 UITabBarController 类中调用它,例如

class CustomTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.inActiveTintColor()
    }
}
Run Code Online (Sandbox Code Playgroud)

您将得到如下输出: 在此输入图像描述 注意:不要忘记将 CustomTabBarViewController 类分配给情节提要中的 TabBarController。