[英]Change TabBar mask color (non-transparent)
I am creating a TabBar with top left and top right corners rounded.我正在创建一个左上角和右上角为圆角的 TabBar。
I'm using a layer mask to achieve this and it works fine, however I need the mask color to be white (its transparent showing the VC background color with the below code).我正在使用图层蒙版来实现这一点,它工作正常,但是我需要蒙版颜色为白色(其透明显示带有以下代码的 VC 背景色)。
Is it possible to set the mask background color white with below approach?是否可以使用以下方法将蒙版背景颜色设置为白色?
I've tried setting layer and layer.mask background colours but with no success (I can't change the VC background color).我试过设置 layer 和 layer.mask 背景颜色但没有成功(我无法更改 VC 背景颜色)。
current code:当前代码:
self.tabBar.layer.masksToBounds = true
self.tabBar.isTranslucent = true
self.tabBar.barStyle = .default
self.tabBar.layer.cornerRadius = 28
self.tabBar.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
Thanks.谢谢。
If you want to set background color to layer mask, you need another layer如果要将背景颜色设置为图层蒙版,则需要另一个图层
Is this the effect you needed?是这个你所需要的效果?
You may try this:你可以试试这个:
extension UITabBar {
func roundCorners(corners: UIRectCorner, backgroundColor: UIColor, cornerColor: UIColor, radius: Int = 20) {
self.backgroundColor = cornerColor
let parentLayer = CALayer()
parentLayer.frame = bounds
parentLayer.backgroundColor = backgroundColor.cgColor
layer.insertSublayer(parentLayer, at: 0)
let maskPath = UIBezierPath(roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.frame = bounds
mask.path = maskPath.cgPath
parentLayer.mask = mask
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.