【问题标题】:How to customize tintColor and resize UITabBarItem如何自定义 tintColor 和调整 UITabBarItem 的大小
【发布时间】:2017-02-04 08:53:47
【问题描述】:

我想为每个标签更改“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()
    }

}

【问题讨论】:

标签: swift resize tabbar uitabbaritem tintcolor


【解决方案1】:

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

switch item.tag{

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

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

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

然后你做你的 indexOfTab 的 switch 语句。

Here is the Swift version.:

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

如果你想单独改变“tintColor”,你应该设置一个自定义的selectedImage。

小心:

默认情况下,未选中和选中的图片会自动创建 来自源图像中的 alpha 值。为防止系统 着色,使用 alwaysOriginal 提供图像。

就documentation 而言,UITabBarItem 没有“tintColor”属性。

但是,UITabBar 本身有一个tintColor property。但这不是单独设置任何东西。

色调颜色

您可以使用 色调 (barTintColor) 字段。默认背景色为白色。

使用图像色调 (selectedImageTintColor) 字段指定条形 选择该选项卡时项目的色调颜色。默认情况下,该颜色是 蓝色。

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

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

【讨论】:

  • 感谢您的回答!所以我应该使用带有 alwaysOriginal 的图像。但是图像大小问题呢?我无法理解每当我触摸标签栏项目时都会调整它的大小。如果我不使用“UIEdgeInsetsMake(12, 10, 11, 11)”,则不会调整它们的大小。但是图像根本不适合 Tabbar 区域。这就是为什么我使用这段代码来拟合它们。你有什么想法吗?
  • @FrozenSea 我用调整大小问题更新了我的答案。您应该首先以正确的尺寸创建图像,而不是在代码中使用 edgeinsets 等进行奇怪的调整大小。在此处查看有关如何更正图像尺寸的完整指南:developer.apple.com/ios/human-interface-guidelines/graphics/…,如果这回答了您的问题,请标记为已回答:) 总帐
  • @FrozenSea 顺便说一句。快速查看您的代码,您的 switch 语句中缺少中断。 switch item.tag{ ,也许这就是你不能改变色调的原因。
  • 我尝试为切换方法添加“中断”。但它没有用。有什么办法可以改变“Item.title”的颜色?
  • @FrozenSea 您的 switch 语句中必须始终有中断。在此处查看标题自定义:stackoverflow.com/questions/28198060/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多