如何快速更改标签栏的色调颜色? [英] How to change tint color of tab bar in swift?

查看:41
本文介绍了如何快速更改标签栏的色调颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标签栏,但有 2 个颜色问题.

I'm using tab bar and I have 2 problems with color.

第一个问题,色调是灰色的,我用一些代码把它改成白色,但只有在按下 tab 时才变成白色.

1st Problem, the tint color is grey, I used some code to change it to white but it turn to white only when tab is pressed.

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

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

    let barColor = UIColor(red: 49/255, green: 75/255, blue: 108/255, alpha: 1.0)
    let pressedTintColor = UIColor.whiteColor()

    UITabBar.appearance().barTintColor = barColor
    UITabBar.appearance().tintColor = pressedTintColor

        return true
}

第二个问题,按下标签的背景颜色应该改变,但它没有改变.

2nd Problem, the background color of pressed tab is supposed to change but it's not changing.

这就是标签栏的样子.

这就是它应该有的样子.

And this is how it's supposed to look.

(第一张图是在Xcode模拟器中作为测试,第二张图是它的设计,所以关于标签的图像和文本并不重要)

(1st pic is in Xcode Simulator just as test, 2nd pic is design of it, so it's not important to much about images and text of tabs)

所以它应该所有的标签都一直是白色的,当一个标签被按下时只改变标签的背景颜色.

So it's supposed all tabs to be all the time white, and when a tab is pressed to change just background color of tab.

推荐答案

要解决 AppDelegate 中的问题,请执行以下操作:

To solve the problems in your AppDelegate do this:

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

    UITabBar.appearance().tintColor = UIColor.whiteColor()
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)

    return true
}

在你的 ViewController 中做这样的事情:

And in your ViewController do something like this:

extension UIImage {
    func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(CGRectMake(0, 0, size.width, size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

extension UIImage {
    func imageWithColor(tintColor: 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, CGBlendMode.Normal)

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

        let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
        UIGraphicsEndImageContext()

        return newImage
    }
}

class FirstViewController: UIViewController {

    var tabBar: UITabBar?

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar = self.tabBarController!.tabBar
        tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height))

        // To change tintColor for unselected tabs
        for item in tabBar!.items! as [UITabBarItem] {
            if let image = item.image {
                item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

*UIImage 的第一个 extension 取自同一作者的另一个问题:如何更改标签栏项目的默认灰色?

*The first extension to UIImage is taken from another question by the same author: How to change default grey color of tab bar items?

这篇关于如何快速更改标签栏的色调颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆