Storyboard中设置UITabbaritem

  • 1方式一:修改图片的渲染色为original
Storyboard中设置UITabbaritem_第1张图片
Snip20170911_5.png

但是在自定义的继承自UITabBarController 的控制器里要加上相应的代码,来改变文字的颜色,在viewDidLoad中

        /*
         
         初始化子控器,如果在Asset.xcassets文件内没有对图片渲染选项进行设置,这里就需要
         设置图片不被渲染,let image = UIImage(named: "xxxooo")
         image.withRenderingMode(.alwaysOriginal)
         */
        
        
        let tabbarItem = UITabBarItem.appearance()
        var attributes : [String:Any] = [:]
        attributes[NSFontAttributeName] = UIFont.systemFont(ofSize: 12)
        attributes[NSForegroundColorAttributeName] = UIColor.lightGray
        tabbarItem.setTitleTextAttributes(attributes, for: .normal)
        
        var attributesSelected = [String:Any]()
        
        attributesSelected[NSFontAttributeName] = UIFont.systemFont(ofSize: 12)
        attributesSelected[NSForegroundColorAttributeName] = UIColor.orange
        tabbarItem.setTitleTextAttributes(attributesSelected, for: .selected)
        

  • 2方法二:在Main.storyboard中添加一个UITabBarController控制面板,然后与自己的控制器关联,在其tabbar属性面板里设置 image tint 为自己想要的颜色
Storyboard中设置UITabbaritem_第2张图片
Snip20170911_4.png
  • 方法三:纯代码设置

        let tabbarItem = UITabBarItem.appearance()
        var attributes : [String:Any] = [:]
        attributes[NSFontAttributeName] = UIFont.systemFont(ofSize: 12)
        attributes[NSForegroundColorAttributeName] = UIColor.lightGray
        tabbarItem.setTitleTextAttributes(attributes, for: .normal)
        
        var attributesSelected = [String:Any]()
        attributesSelected[NSFontAttributeName] = UIFont.systemFont(ofSize: 12)
        attributesSelected[NSForegroundColorAttributeName] = UIColor.orange
        tabbarItem.setTitleTextAttributes(attributesSelected, for: .selected)
        
        
        let image = UIImage(named: "xxxooo")
        let selectedImage = UIImage(named: "xxxooo_select")
        
        tabbarItem.image = image?.withRenderingMode(.alwaysOriginal)
        tabbarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)

你可能感兴趣的:(Storyboard中设置UITabbaritem)