gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:18:07 25 4
gpt4 key购买 nike

我想为每个选项卡更改“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 语句中缺少中断:

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相反。

注意:

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本身有一个 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相反,或者 check this question如果它确实满足您的需求。但是,UITabBar 和 UITabBarItem 自定义仅限于您可以在文档中阅读的内容。

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

关于swift - 如何自定义 tintColor 和调整 UITabBarItem 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42038402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com