【问题标题】:How to change color for tab bar non selected icon in swift?如何快速更改标签栏未选择图标的颜色?
【发布时间】:2015-07-04 18:48:10
【问题描述】:

如何更改标签栏未选中图标和文本的颜色?我找到了这个答案 (How to change inactive icon/text color on tab bar?),但无法快速实现。

【问题讨论】:

    标签: ios swift uitabbaritem


    【解决方案1】:

    以下设置所有 UITabBarItem 的默认值,您可以将其添加到您的 AppDelegate。它会改变你的文字颜色。

    UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blackColor()}, forState:.Selected)
    UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.whiteColor()}, forState:.Normal)
    

    要更改图标的颜色,您可以将图像设置为图像已经具有良好颜色的给定状态。

    self.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectedImage"]
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.tabBarItem.image = [[UIImage imageNamed:@"notSelectedImage"] 
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

    或者你可以这样做:

    为UIImage 类添加扩展名(来自this answer):

    extension UIImage {
    func imageWithColor(color1: 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, kCGBlendModeNormal)
    
        let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
        CGContextClipToMask(context, rect, self.CGImage)
        color1.setFill()
        CGContextFillRect(context, rect)
    
        let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
        UIGraphicsEndImageContext()
    
        return newImage
    }
    }
    

    在你的viewDidLoad:

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

    【讨论】:

      【解决方案2】:

      补充@BoilingLime 的答案,这里是 Swift 3 中的第二个替代 UIImage 扩展:

      extension UIImage {
      func imageWithColor(color1: 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!)
          color1.setFill()
          context.fill(rect)
      
          let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
          UIGraphicsEndImageContext()
      
          return newImage
      }
      }
      

      【讨论】:

        【解决方案3】:

        iOS 10

        class TabBarVC: UITabBarController {
        
            override func viewDidLoad() {
                super.viewDidLoad()
        
                // make unselected icons white
                self.tabBar.unselectedItemTintColor = UIColor.white
            }
        }
        

        【讨论】:

        • 也可以放在 AppDelegate 中。
        • 我如何在 iOS 9 上使用它?
        【解决方案4】:

        如果您正在寻找 iOS 11 swift 4 解决方案,请在 appDelegate 中执行类似的操作。这会将所有未选中的标签栏项目更改为黑色。

            func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
        
            UITabBar.appearance().unselectedItemTintColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 1)
        
            return true
        }
        

        【讨论】:

          【解决方案5】:

          在 iOS 11 中,您可以直接在 storyboard 的 UIToolBar 中设置属性:

          unselectedItemTintColor |颜色 | [想要的颜色]

          Xcode 打印

          【讨论】:

            【解决方案6】:

            Swift 4+

            UITabBar.appearance().unselectedItemTintColor = UIColor.green
            

            在appDelegate's didFinishLaunchingWithOptions 方法中使用此代码。

            【讨论】:

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