更改选项卡栏背景透明度
问题描述:
我想让我的选项卡栏在我的选项卡栏控制器有一个低不透明背景,所以它是半透明的,我试图以编程方式做到这一点,但背景更改为正确的颜色,但总是显得坚实,没有透明度。更改选项卡栏背景透明度
这里是我的TabBarViewController
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.unselectedItemTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.4)
self.tabBar.barTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.5)
// Do any additional setup after loading the view.
}
}
答
代码只是backgroundColor
取代barTintColor
。
override func viewDidLoad() {
super.viewDidLoad()
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.5)
// Do any additional setup after loading the view.
}
答
对于这种情况,你应该产生一个定制UIImage
标签栏backgroundimage
财产。
让我们假设你的标签栏您想要的颜色 - 即应transparent-是黑色的,你可以在自定义UITabBarController
实现:
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let transperentBlackColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.5)
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
transperentBlackColor.setFill()
UIRectFill(rect)
if let image = UIGraphicsGetImageFromCurrentImageContext() {
tabBar.backgroundImage = image
}
UIGraphicsEndImageContext()
}
}
因此,作为一个输出,如果我们假定背景您的视图控制器的颜色是蓝色,它应该是这样的:
如图所示,标签栏不是纯黑色,它包含如预期的透明度(0.5)。
此外,为了检查如何生成纯色UIImage
,您可以查看此回答:Create UIImage with solid color in Swift。
嗨,感谢您的答案,尝试但仍然显示纯色 –