How to change tint color of tab bar in swift?

前端 未结 1 963
攒了一身酷
攒了一身酷 2021-01-19 01:37

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

1st Problem, the tint color is grey, I used some code to change it to white but it turn to whit

相关标签:
1条回答
  • 2021-01-19 02:35

    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
    }
    

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

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

    0 讨论(0)
提交回复
热议问题