加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 02:02:17 所属栏目:百科 来源:网络整理
导读:环境: – Xcode 6 beta 4 – Swift语言 – iOS标签应用程序(默认xCode项目) 如何将标签的默认灰色更改为其他? (优选全球) 就我的研究而言,我需要以某种方式将每个标签的图像渲染模式更改为原始渲染模式,但我不知道如何 解决方法 每个(默认)标签栏项由文
环境:
– Xcode 6 beta 4
– Swift语言
– iOS标签应用程序(默认xCode项目)

如何将标签的默认灰色更改为其他? (优选全球)

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

解决方法

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

// 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)

与图像情况有点复杂。您无法在全局范围内定义其外观。您应该在TabBarController类中重新定义它们。添加代码到以下TabBarController类的viewDidLoad方法:

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

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

// 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,self.size.height)
        CGContextScaleCTM(context,1.0,-1.0);
        CGContextSetBlendMode(context,.Normal)

        let rect = CGRectMake(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
    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读