无法将颜色更改为 tabBar? [英] Unable to change the color to tabBar?

查看:26
本文介绍了无法将颜色更改为 tabBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 parantTabBarController 类中有以下方法:可以看到各种尝试使 tabBar 完全透明.唯一有效的是顶部的那个.

I have the following method in the parantTabBarController class: There can be seen various attempts made to make the tabBar completely transparent. The only one that worked is the one found at the top.

       override func viewDidLoad() {
        super.viewDidLoad()

        UITabBar.appearance().barTintColor = UIColor.clear
        UITabBar.appearance().backgroundImage = UIImage()
//        UITabBar.appearance().barTintColor = UIColor.blue

//        changeTabBarOpacity()
//        self.tabBar.unselectedItemTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.4)
//        self.tabBar.backgroundColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.0)

//        self.tabBar.backgroundColor = UIColor.clear
//        self.tabBar.backgroundImage = UIImage()
//        self.tabBar.shadowImage = UIImage()  // removes the border

    }

但是,使用这种方法,我无法在其他视图控制器中更改同一个 tabBar 的背景颜色.我尝试用白色图像替换图像,更改背景颜色: UITabBar.appearance().backgroundColor = UIColor.white 但没有任何效果.

However with this approach I am not able to change the background color of this same tabBar in other view controllers. I have tried replacing the image with a white image, changing the background color: UITabBar.appearance().backgroundColor = UIColor.white But nothing works.

如何在一个页面上有一个半透明的 tabBar 而在所有其他页面上有一个白色的?

推荐答案

斯威夫特 5:

好的,我找到了解决方案,主要的关键是使用 UITabBarisTranslucent 属性.

Okay i found the solution, the main key is using the isTranslucent property of UITabBar.

  • 如果您将 isTranslucent : true 发送到具有不透明自定义的标签栏背景图像选项卡栏将对图像应用小于 1.0 的系统不透明度.

如果你想设置清晰的颜色,那么你只需要将 isTranslucent 设置为 true.如果您想应用其他颜色,请将 isTranslucent 设置为 false.

if you want to set clear color then u just have to set isTranslucent to true only. and if you want to apply other colors then set isTranslucent to false.

将下面的 TabBarViewController 类用于您的 TabBarViewController

Use the below TabBarViewController class to your TabBarViewController

    class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.delegate = self
            
            self.tabBar.isTranslucent = true
            UITabBar.appearance().backgroundImage = UIImage()
            
            //This is for removing top line from the tabbar.
            UITabBar.appearance().layer.borderWidth = 0.0
            UITabBar.appearance().clipsToBounds = true
        }
    
       // This method will get called when you tap on any tab
        func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
            
            if viewController == tabBarController.viewControllers?[0] { //<----- This is first viewController
                
                //If you set isTranslucent to true then no need to set barTintColor. it will make your tabBar transparent
                self.tabBar.isTranslucent = true
                
            } else if viewController == tabBarController.viewControllers?[1] { //<----- This is second viewController
                
                self.tabBar.isTranslucent = false
                
               // the tab bar will provide an opaque background black for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
                self.tabBar.barTintColor = .white
                
                // OR
                
              //  self.tabBar.barTintColor = nil
                
            } else {
                self.tabBar.isTranslucent = false
                self.tabBar.barTintColor = .red
            }
            return true
        }
    }

输出:-

希望能帮到你

这篇关于无法将颜色更改为 tabBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆