Unable to change the color to tabBar?

本秂侑毒 提交于 2020-05-28 09:54:46

问题


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

    }

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.

How can I have a translucent tabBar on one page and a white one on all others?


回答1:


@isa123 try this code in appdelegate didFinishLaunchingWithOptions method

  UITabBar.appearance().tintColor = .white
  UITabBar.appearance().barTintColor = UIColor(named: "PrimaryDark")
  UITabBar.appearance().isOpaque = false
  UITabBar.appearance().backgroundImage = UIImage.init(color: UIColor(named: "PrimaryDark")!, size: CGSize(width: 1, height: 1))



回答2:


Swift 5 :

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

  • If you send isTranslucent : true to a tab bar with an opaque custom background image the tab bar will apply a system opacity less than 1.0 to the image.

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.

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
        }
    }

Output : -

Hope this helps



来源:https://stackoverflow.com/questions/61811044/unable-to-change-the-color-to-tabbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!