如何自定义 tintColor 和调整 UITabBarItem 的大小

How to customize tintColor and resize UITabBarItem

我想为每个选项卡更改 "Tintcolor" 的问题。但是下面的代码根本不起作用。

我添加了按钮图像并想使用 "UIEdgeInsetsMake" 调整它的大小。但是每当我触摸按钮时,按钮都会奇怪地调整大小。不知道为什么。

我正在使用 Swift 3.

   class MainView: UITabBarController {

    var TabFirst = UITabBarItem()
    var TabSecond = UITabBarItem()
    var TabThird = UITabBarItem()
    var TabForth = UITabBarItem()
    var TabFifth = UITabBarItem()

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.barTintColor = UIColor.white

        TabFirst = self.tabBar.items![0]
        TabFirst.image = UIImage(named: "btn_1-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        TabFirst.imageInsets = UIEdgeInsetsMake(12, 10, 11, 11)
        tabBar.items?[0].title = "length"

        TabSecond = self.tabBar.items![1]
        TabSecond.image = UIImage(named: "btn_2-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        tabBar.items?[1].title = "length"

        TabThird = self.tabBar.items![2]
        TabThird.image = UIImage(named: "btn_3-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        tabBar.items?[2].title = "length"

        TabForth = self.tabBar.items![3]
        TabForth.image = UIImage(named: "btn_4-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        tabBar.items?[3].title = "length"

        TabFifth = self.tabBar.items![4]
        TabFifth.image = UIImage(named: "btn_5-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        tabBar.items?[4].title = "length"

    }



    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        switch item.tag{
        case 0:
            print("FirstTab")
            UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 67/255.0, blue: 99/255.0, alpha: 1.0)

        case 1:
            print("SecondTab")
            UITabBar.appearance().tintColor = UIColor(red: 237/255.0, green: 193/255.0, blue: 53/255.0, alpha: 1.0)

        case 2:
            print("ThirdTab")
            UITabBar.appearance().tintColor = UIColor(red: 70/255.0, green: 183/255.0, blue: 128/255.0, alpha: 1.0)

        case 3:
            print("ForthTab")
            UITabBar.appearance().tintColor = UIColor(red: 12/255.0, green: 195/255.0, blue: 199/255.0, alpha: 1.0)

        case 4:
            print("FifthTab")
            UITabBar.appearance().tintColor = UIColor(red: 105/255.0, green: 72/255.0, blue: 170/255.0, alpha: 1.0)

        default:
            break
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        UIApplication.shared.isStatusBarHidden = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

编辑: 您在 switch 语句中缺少 breaks:

switch item.tag{

此外,您正在对标签进行切换,但我没有看到您在代码中对它们进行相应标记的任何地方。您应该获取项目的索引。

我不是 Swift 编码员,这是你在 Objective-C 中的做法,给你一个提示:

NSInteger indexOfTab = [[self.tabBar items] indexOfObject:item];

然后执行 indexOfTab 的 switch 语句。

:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("the selected index is : \(tabBar.items.index(of: item))")
}

如果您想单独更改 "tintColor" ,您应该改为设置自定义 selectedImage

注意:

By default, unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with alwaysOriginal.

documentation 而言,UITabBarItem 没有 "tintColor" 属性。

不过,UITabBar itself has a tintColor property。但这是 而不是 单独设置任何内容。

Tint Color

You can specify a custom tint color for the bar background using the Tint (barTintColor) field. The default background tint color is white.

Use the Image Tint (selectedImageTintColor) field to specify the bar item’s tint color when that tab is selected. By default, that color is blue.

关于调整大小的方法,如果它符合您的需要,您应该 resize your original image instead, or 。但是,UITabBar 和 UITabBarItem 自定义仅限于您可以在文档中阅读的内容。

如果您想进一步单独自定义内容,我建议您改为搜索或创建自定义解决方案。