【问题标题】:How to change tint color of tab bar in swift?如何快速更改标签栏的色调?
【发布时间】:2015-11-07 14:12:34
【问题描述】:

我正在使用标签栏,但我有 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
}

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

这就是标签栏的外观。

这就是它应该的样子。

(第 1 张是 Xcode Simulator 中的测试,第 2 张是设计的,所以标签的图像和文本并不重要)

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

【问题讨论】:

    标签: swift uitabbarcontroller xcode7 tabbar


    【解决方案1】:

    要解决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.
        }
    }
    

    *第一个extensionUIImage取自同一作者的另一个问题:How to change default grey color of tab bar items?

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2015-07-04
      • 1970-01-01
      • 2020-10-27
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      相关资源
      最近更新 更多