更改TabBar遮罩颜色(不透明)

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

我正在创建一个TabBar,它的左上角和右上角是圆的。

我正在使用图层蒙版来实现此目的,并且它工作正常,但是我需要将蒙版颜色设置为白色(透明,以下面的代码显示VC背景色)。

是否可以通过以下方法将蒙版背景色设置为白色?

我尝试设置layer和layer.mask背景颜色,但没有成功(我无法更改VC背景颜色)。

当前代码:

self.tabBar.layer.masksToBounds = true
self.tabBar.isTranslucent = true
self.tabBar.barStyle = .default
self.tabBar.layer.cornerRadius = 28
self.tabBar.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

谢谢。

ios swift xcode tabbar
1个回答
0
投票

如果要将背景色设置为图层蒙版,则需要另一个图层

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
}
}
© www.soinside.com 2019 - 2024. All rights reserved.