ios 使用 Swift Xcode 6 的默认标签栏项目颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25052729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:36:54  来源:igfitidea点击:

Default tab bar item colors using swift Xcode 6

iosxcodeswiftxcode6

提问by Nick Germi

Environment: - Xcode 6 beta 4 - Swift language - iOS Tabbed Application (default xCode project)

环境: - Xcode 6 beta 4 - Swift 语言 - iOS 选项卡式应用程序(默认 xCode 项目)

How can I change the default grey color of the tabs to something else? (Preferably globally)

如何将选项卡的默认灰色更改为其他颜色?(最好是全局的)

As far as my research goes I need to somehow change the image rendering mode for each tab to Original rendering mode however I don't know how

就我的研究而言,我需要以某种方式将每个选项卡的图像渲染模式更改为原始渲染模式,但我不知道如何

回答by Keenle

Each (default) tab bar item consists of text and icon. It is pretty easy to change the text colors globally by specifying the appearance:

每个(默认)标签栏项目由文本和图标组成。通过指定外观来全局更改文本颜色非常容易:

// you can add this code to you AppDelegate application:didFinishLaunchingWithOptions: 
// or add it to viewDidLoad method of your TabBarController class
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)

With images situation is a little bit more complicated. You cannot define their appearance globally. You should redefine them in your TabBarController class. Add code bellow to viewDidLoadmethod of your TabBarControllerclass:

使用图像的情况稍微复杂一些。您不能全局定义它们的外观。您应该在 TabBarController 类中重新定义它们。将下面的代码添加到viewDidLoad您的TabBarController类的方法中:

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

As we know there is no imageWithColor(...)method in UIImage class. So here is the extension implementation:

我们知道imageWithColor(...)UIImage 类中没有方法。所以这是扩展实现:

// Add anywhere in your app
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, .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
    }
}

imageWithColorwas borrowed from this answer: https://stackoverflow.com/a/24545102/3050466

imageWithColor是从这个答案借来的:https: //stackoverflow.com/a/24545102/3050466

回答by pulp

I don't have enough reputation for commenting the comments, but many are interested how to change the color of selected image

我没有足够的评论评论的声誉,但许多人对如何更改所选图像的颜色感兴趣

just add another if letcheck after

只需if let在之后添加另一张支票

if let image = item.image

just like this:

像这样:

if let selectedImage = item.selectedImage {
            item.selectedImage = selectedImage.imageWithColor(UIColor.yellowColor()).imageWithRenderingMode(.AlwaysOriginal)
        }

this solved the problem perfectly. And a little addition, since Swift 1.2 and Xcode 6.3.2 you need

这完美地解决了这个问题。还有一点补充,因为 Swift 1.2 和 Xcode 6.3.2 你需要

for item in self.tabBar.items as! [UITabBarItem]

instead of

代替

for item in self.tabBar.items as [UITabBarItem]

Hope that helps!

希望有帮助!

回答by Eli Stone

Swift 2.0

斯威夫特 2.0

To change the default color for tab bar images, Add code bellow to viewDidLoadmethod of your TabBarControllerclass:

要更改标签栏图像的默认颜色viewDidLoad,请在TabBarController类的方法中添加以下代码:

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

Update the imageWithColorextension. Used with the above method and should be placed outside of your TabBarControllerclass:

更新imageWithColor扩展。与上述方法一起使用,应该放在你的TabBarController类之外:

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
    }
}

No changes to the way text gets coloured but just for reference. Also should be added the code bellow to viewDidLoad:

文本着色方式没有变化,仅供参考。还应该将下面的代码添加到viewDidLoad

// you can add this code to you AppDelegate application:didFinishLaunchingWithOptions: 
// or add it to viewDidLoad method of your TabBarController class
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)

回答by Dasoga

Swift 3.0

斯威夫特 3.0

To change the default color for tab bar images, Add code bellow to viewDidLoadmethod of your TabBarControllerclass:

要更改标签栏图像的默认颜色viewDidLoad,请在TabBarController类的方法中添加以下代码:

    for item in self.tabBar.items! as [UITabBarItem] {
        if let image = item.image {
            item.image = image.imageWithColor(tintColor: UIColor.yellow).withRenderingMode(.alwaysOriginal)
        }
    }

Update the imageWithColorextension. Used with the above method and should be placed outside of your TabBarControllerclass:

更新imageWithColor扩展。与上述方法一起使用,应该放在你的TabBarController类之外:

extension UIImage {
 func imageWithColor(tintColor: 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!)
    tintColor.setFill()
    context.fill(rect)

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

    return newImage
 }
}