2017-02-04 28 views
1

我想为每个选项卡更改“Tintcolor”的问题。但下面的代码根本不起作用。如何定制tintColor并调整大小UITabBarItem

我添加了按钮图像,并希望使用“UIEdgeInsetsMake”调整它的大小。但是,只要我按下按钮,按钮就会被奇怪地调整大小。我不知道为什么。

,我使用斯威夫特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() 
    } 

} 
+0

http://stackoverflow.com/questions/27890936/changing-selectedimage-on-uitabbaritem-in-swift你应该改变选定的图像,而不是如果你想要色调:http://stackoverflow.com/questions/ 20783193/setting-tint-color-for-selected-tab-in-uitabbar – 2017-02-04 08:56:30

+0

@Sneak我只想改变TintColor,而不是改变图像的方式。有什么方法可以通过使用编程代码来改变它吗?不使用故事板菜单?而且我也很想解决调整尺寸的问题。 –

回答

0

编辑: 你缺少休息在switch语句:

switch item.tag{ 

而且,你是在做一个开关标签,我没有看到任何地方你已经在你的代码中相应地标记了它们。您应该获得该项目的索引。

我不是斯威夫特编码器,这是你如何做到这一点在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代替。

当心:

默认情况下,未选择的和所选择的图像被自动创建从源图像中的α值 。为防止系统着色,请提供alwaysOriginal的图像。

documentation而言,UITabBarItem没有“tintColor”属性。但是,UITabBar本身具有tintColor property。但这是而不是单独设置任何东西。

色调颜色

您可以指定使用 色调(barTintColor)场酒吧背景自定义色彩的颜色。默认背景色调是白色。

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

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

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

+0

谢谢你的回答!所以我应该使用alwaysOriginal的图像。但是,图像大小问题呢?我无法理解,每当我触摸它时,Tabbar项目都会调整大小。如果我不使用“UIEdgeInsetsMake(12,10,11,11)”,它们不会调整大小。但这些图像根本不适合Tabbar区域。这就是为什么我使用这个代码来拟合它们。你有什么想法吗? –

+0

@FrozenSea我更新了我的答案与调整大小的问题。您应该先创建大小适当的图片,而不是在代码中使用边缘渐变等进行大小调整。请在此处查找有关如何更正图片大小的完整指南:https://developer.apple.com/ios/human -interface-guidelines/graphics/image-size-and-resolution /,如果这回答你的问题,请标记为已回答:) GL – 2017-02-04 09:24:41

+0

@FrozenSea BTW。快速查看你的代码,你在switch语句中缺少了断点。切换item.tag {,也许这就是为什么你不能改变色调。 – 2017-02-04 09:31:21

相关问题