UItabar不能更改颜色或透明

时间:2018-07-18 09:03:29

标签: ios swift uitabbarcontroller

我已经实现了以下代码,以使tabBar透明,但是它根本没有生效,我错过了什么吗?我在UItabbarcontroller中有此代码。

 mainTabbar.barTintColor = .clear
 mainTabbar.tintColor = .black

 mainTabbar.backgroundImage = UIImage()
 mainTabbar.shadowImage = UIImage()

2 个答案:

答案 0 :(得分:1)

使用此

func configureTabBar(tabBarController:UITabBarController) {
        //Tab bar customization
        tabBarController.tabBar.backgroundImage = coloredImage(size: UIScreen.main.bounds.size, restrictedToRect: CGRect(x: 0, y: 0, width: (tabBarController.tabBar.frame.size.width),height: (tabBarController.tabBar.frame.size.height)), color: UIColor.clear)
        tabBarController.tabBar.shadowImage = UIImage()
    }

然后使用此方法创建透明色UIImage

func coloredImage(size:CGSize,restrictedToRect:CGRect,color:UIColor) -> UIImage{

    let colorView = UIView(frame: CGRect(origin:CGPoint(x: 0, y: 0) , size: size))
    colorView.backgroundColor = color
    colorView.contentScaleFactor = UIScreen.main.scale

    UIGraphicsBeginImageContextWithOptions(restrictedToRect.size, false, UIScreen.main.scale)
    colorView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

使用示例

if let tabBarController = self.window?.rootViewController as? UITabBarController {
        self.configureTabBar(tabBarController: tabBarController)
}

答案 1 :(得分:0)

在UITabbarViewController类中

self.tabBar.barTintColor = .clear
self.tabBar.tintColor = .black