简体   繁体   中英

Change TabBar mask color (non-transparent)

I am creating a TabBar with top left and top right corners rounded.

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).

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).

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
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM