无法更改tabBar的颜色?

问题描述 投票:0回答:1

我在parantTabBarController类中有以下方法。可以看到我们尝试了很多方法来让tabBar完全透明。唯一有效的是在顶部找到的那个方法。

       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 但都没有用。

怎样才能在一个页面上有一个半透明的tabBar,而在其他页面上有一个白色的tabBar呢?

ios swift uitabbarcontroller tabbar
1个回答
0
投票

@isa123 在appdelegate的didFinishLaunchingWithOptions方法中试试这段代码。

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

0
投票

Swift 5 。

好吧,我找到了解决方案,主要的关键是使用了 isTranslucent 的财产 UITabBar.

  • 如果你发送isTranslucent : true到具有不透明自定义背景图像的tab栏,tab栏将对图像应用小于1.0的系统不透明度。

如果你想设置清晰的颜色,那么你只需要设置为 isTranslucent 如果你想应用其他颜色,则将 isTranslucent 改为false.使用下面的TabBarViewController类到你的TabBarViewController中。

使用下面的TabBarViewController类到你的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
        }
    }

输出:-

enter image description here

enter image description here

enter image description here

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.