简体   繁体   English

更改 TabBar 蒙版颜色(非透明)

[英]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.

相关问题 iOS-仅在非透明区域上用颜色覆盖UIImage? - iOS - Overlay UIImage with color on non-transparent area only? 如何在 SwiftUI 中使标签栏外观不透明,而不在 UIKit 属性中点击? - How to make tabbar appearance non-transparent in SwiftUI, without tapping in UIKit properties? 具有非透明子视图的iOS 8透明视图 - iOS 8 Transparent View with non-transparent subviews 透明视图在另一个透明的UIView上有不透明的内容? - Transparent view on another transparent UIView with non-transparent content? 如何检测UIImage的非透明部分何时与UIImage的另一个非透明部分接触 - How to detect when the non-transparent part of an UIImage is in contact with another non-transparent part of an UIImage 如何创建具有非透明边框的透明UIView - How to create transparent UIView with non-transparent border 在Swift3中仅触摸SKSpriteNode的非透明像素 - Touch only on non-transparent pixels of SKSpriteNode in Swift3 有效地检测仅触摸UIImageView的非透明像素 - Detect touches only on non-transparent pixels of UIImageView, efficiently 核心图形 - 如何从UIImage中裁剪非透明像素? - Core Graphics - how to crop non-transparent pixels out of a UIImage? 非透明状态栏,不显示导航栏 - Non-transparent Status Bar without displaying navigation bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM