简体   繁体   English

如何快速更改选项卡栏未选中图标的颜色?

[英]How to change color for tab bar non selected icon in swift?

How to change color for tab bar non selected icon and text? 如何更改选项卡栏未选中图标和文本的颜色? I found this answer ( How to change inactive icon/text color on tab bar? ), but can't implement it for swift. 我找到了这个答案( 如何更改选项卡栏上非活动图标/文本的颜色? ),但无法快速实现。

iOS 10 iOS 10

class TabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // make unselected icons white
        self.tabBar.unselectedItemTintColor = UIColor.white
    }
}

The below sets the defaults for all UITabBarItem's, you can add it to your AppDelegate . 下面为所有UITabBarItem设置了默认值,您可以将其添加到您的AppDelegate It will change your text color. 它将更改您的文本颜色。

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blackColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.whiteColor()}, forState:.Normal)

For changing the icon' color you can either set the image for the given state where your image already have the good color. 要更改图标的颜色,您可以将图像设置为图像已经具有良好颜色的给定状态。

self.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectedImage"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.tabBarItem.image = [[UIImage imageNamed:@"notSelectedImage"] 
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Or you can do it this way : 或者,您可以通过以下方式进行操作:

Add an extension to UIImage class (from this answer ) : UIImage类添加扩展(从此答案开始 ):

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

    let context = UIGraphicsGetCurrentContext() as CGContextRef
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal)

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

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
}
}

And in your viewDidLoad : 在您的viewDidLoad

for item in self.tabBar.items as [UITabBarItem] {
    if let image = item.image {
        item.image = image.imageWithColor(UIColor.blackColor()).imageWithRenderingMode(.AlwaysOriginal)
    }
}

In iOS 11 you can set the property directly at UIToolBar in storyboard: 在iOS 11中,您可以直接在情节提要中的UIToolBar上设置属性:

unselectedItemTintColor | unselectedItemTintColor | Color | 颜色| [desired color] [所需颜色]

Xcode print Xcode打印

屏幕截图

Complementing @BoilingLime's answer, here it goes the second alternative's UIImage extension in Swift 3: 补充@BoilingLime的答案,在这里它是Swift 3中第二种替代方法的UIImage扩展:

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

    let context = UIGraphicsGetCurrentContext()! as CGContext
    context.translateBy(x: 0, y: self.size.height)
    context.scaleBy(x: 1.0, y: -1.0);
    context.setBlendMode(CGBlendMode.normal)

    let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
    context.clip(to: rect, mask: self.cgImage!)
    color1.setFill()
    context.fill(rect)

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

    return newImage
}
}

Swift 4+ 迅捷4+

UITabBar.appearance().unselectedItemTintColor = UIColor.green

Use this code in appDelegate's didFinishLaunchingWithOptions method. appDelegate's didFinishLaunchingWithOptions方法中使用此代码。

If you are looking for an iOS 11 swift 4 solution, do something like this in the appDelegate. 如果您正在寻找iOS 11 swift 4解决方案,请在appDelegate中执行类似的操作。 This is changing all the unselected tab bar items to black. 这会将所有未选中的选项卡栏项目更改为黑色。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UITabBar.appearance().unselectedItemTintColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 1)

    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM