swift - 如何快速更改标签栏的色调颜色?

标签 swift uitabbarcontroller xcode7 tabbar

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

第一个问题,色调是灰色的,我用了一些代码将其更改为白色,但只有在按下 tab 时它才会变成白色。

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
}

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

This is how tab bar looks. enter image description here

And this is how it's supposed to look.enter image description here

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

所以它应该是所有选项卡始终是白色的,并且当按下一个选项卡以更改选项卡的背景颜色时。

最佳答案

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

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 中执行如下操作:

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 取自同一作者的另一个问题:How to change default grey color of tab bar items?

关于swift - 如何快速更改标签栏的色调颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33583693/

相关文章:

iphone - 在 UITabBarController 中的 UIWebView 中支持 HTML5 视频的横向模式

ios - 无法将我的 Swift 类访问到 Objective-C 应用程序中

ios - “项目名称”是通过优化编译的——步进可能表现得很奇怪;变量可能不可用

swift - 缩略图图像未显示在 MKMapView 注释 View 中

ios - 贪婪的 UITabBarController?

iphone - 在没有私有(private) API 的情况下实现自定义 UITabBarController

Xcode 7 错误 ITMS-90474 : "Invalid Bundle", 无法提交给 Apple

swift - 为什么如果我为导航 Controller 栏设置颜色,它看起来与 Photoshop 或任何应用程序不同?

ios - 如何获得多色边框?

Swift:collectionviewHeader 委托(delegate)不起作用?