UItabar不能变色或透明
UItabar can not change colour or transparent
我已经实现了下面的代码,使 tabBar
透明,但它根本没有生效,我错过了什么吗?我在 UItabbarcontroller
.
中有这段代码
mainTabbar.barTintColor = .clear
mainTabbar.tintColor = .black
mainTabbar.backgroundImage = UIImage()
mainTabbar.shadowImage = UIImage()
在 UITabbarViewController 中 class
self.tabBar.barTintColor = .clear
self.tabBar.tintColor = .black
使用这个
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)
}
我已经实现了下面的代码,使 tabBar
透明,但它根本没有生效,我错过了什么吗?我在 UItabbarcontroller
.
mainTabbar.barTintColor = .clear
mainTabbar.tintColor = .black
mainTabbar.backgroundImage = UIImage()
mainTabbar.shadowImage = UIImage()
在 UITabbarViewController 中 class
self.tabBar.barTintColor = .clear
self.tabBar.tintColor = .black
使用这个
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)
}