如何在“标签栏”色调颜色中设置2种不同的颜色?

时间:2018-08-06 05:20:48

标签: ios objective-c uitabbarcontroller

我有一个带有图像和标题的标签栏。如何将选定的选项卡栏项目图像设置为渐变颜色?

当前

enter image description here

预期结果和原始选择的图像

enter image description here

我到目前为止所做的:-

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UITabBar appearance] setTintColor:ThemeBlueColor];
}

是否有任何方法可以设置[[UITabBar appearance] setTintColor:ThemeBlueColor];的渐变颜色?请帮忙。谢谢。

2 个答案:

答案 0 :(得分:1)

我确实更改了选项卡按钮属性(如标题颜色)以及使用目标C代码选择和取消选择的图像,并使用Swiftify * 提供了 *转换后的代码。

在您的第一个ViewController的tabBarController的viewDidLoad()中调用此方法,您就很好了

请看一下-:

                    ` public void onClick(View v) {
                          for(int i = 0;i<parent.getChildCount();i++){
                               if(parent.getChildAt(i)instanceof EditText){
                                  passwordView = (EditText)parent.getChildAt(i);
                                }
                           }
                      }`

类似的Objective-C等效项-:

//  Converted with Swiftify v1.0.6472 - https://objectivec2swift.com/
func setTabBarSelectedDeselectedIconsAndTitleColor() {
    let recentItem = tabBarController?.tabBar.items[0] as? UITabBarItem
    recentItem?.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .normal)
    recentItem?.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .selected)
    recentItem?.image = UIImage(named: "home_unselect_icon.png")?.withRenderingMode(.alwaysOriginal)
    recentItem?.selectedImage = UIImage(named: "home_select_icon.png")?.withRenderingMode(.alwaysOriginal)
    recentItem = tabBarController?.tabBar.items[1]
    recentItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .normal)
    recentItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .selected)
    recentItem.image = UIImage(named: "kid_location_unselect_icon.png")?.withRenderingMode(.alwaysOriginal)
    recentItem.selectedImage = UIImage(named: "kid_location_select_icon.png")?.withRenderingMode(.alwaysOriginal)
}

答案 1 :(得分:0)

您以渐变色获取图像资源,对于选定的图像,只需分别设置该图像或未选择的图像即可。