美文网首页Swift编程iOS 开发继续加油
Swift进阶之UITabBarItem样式修改

Swift进阶之UITabBarItem样式修改

作者: 大脸猫121 | 来源:发表于2016-06-21 16:28 被阅读2713次

    关于创建tabBar和视图控制器的部分就不啰嗦了,我们来研究一下样式的修改( ⊙o⊙ )

    一、修改图片和文字的颜色

    (1)图片和文字同时修改

      self.tabBar.tintColor = UIColor.orangeColor()
    

    (2)只改变文字的颜色

      UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState:.Normal)
      UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState:.Selected)
    

    (3)只改变图片的颜色

    self.tabBar.tintColor = UIColor.orangeColor()
    //文字颜色还原
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: self.view.tintColor], forState:.Selected)
    

    二、选中时、不选中时使用不同的图片

    在默认的item选中、不选中使用的是同一张照片,只是颜色不同。其实我们也可以使用两张不同图片表示两种状态。
    apple.tabBarItem = UITabBarItem(title: "apple", image: UIImage(named: "apple"),selectedImage: UIImage(named: "apple_active"))
    android.tabBarItem = UITabBarItem(title: "Android", image: UIImage(named: "android"),selectedImage: UIImage(named: "android_active"))
    

    三、使用图片的原始颜色

    默认情况下,不管原图是什么颜色,渲染器都会讲渲染成单一颜色。
    apple.tabBarItem =  UITabBarItem(title: "apple", image: UIImage(named: "apple_color"),selectedImage: UIImage(named:"apple_color")?.imageWithRenderingMode(.AlwaysOriginal))
    android.tabBarItem =  UITabBarItem(title: "Android", image: UIImage(named: "android_color"),selectedImage: UIImage(named: "android_color")?.imageWithRenderingMode(.AlwaysOriginal))

    相关文章

      网友评论

        本文标题:Swift进阶之UITabBarItem样式修改

        本文链接:https://www.haomeiwen.com/subject/vbsgdttx.html