设置 tabBar 下面文字和图片颜色

来源:互联网 发布:java获取资源文件路径 编辑:程序博客网 时间:2024/04/28 12:52

设置 tabBar 下面文字和图片颜色

设置图片

1.统一设置

tabBarItem.image =  XXimage

2.显示为图片原样

**设置--图片渲染模式为origin**
        childViewController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)

设置文字和大小颜色

  • 可以定制每一个自控制器的颜色 tabBarItem.setTitleTextAttributes–传入一个字典(里面设置文字颜色和大小)
childViewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)

tabBar 下面文字与对应的导航栏 title 文字设置

  • 事例代码
        childViewController.title = title        childViewController.navigationItem.title = title        childViewController.tabBarItem.title = title
  • 如果用上面一句就会设置成统一的文字
  • 如果用下面两句,则可以实现 tabBar 文字与导航栏 title 不同

如果tabBar下面的 image 中包括文字,此时不需要 tabBar 显示文字

  • 此时问题就是— image 会整体上移动
    *解决方法:调整 image 位置
    设置图片的 imageInsets
        //tabBarItem的 Insets top向上稍微移动就好        childViewController.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: 0, right: 0)
0 0