简体   繁体   English

如何将特定角和阴影设置为 UIView

[英]How to set specific corner and shadow to UIView

I am trying to set top left and top right corners to tab bar along with shadow.我正在尝试将标签栏的左上角和右上角与阴影一起设置。

I am using below function which is in my UIView extension:我在下面使用 function ,它在我的 UIView 扩展中:

func addShadowWithCurve(usingCorners corners : UIRectCorner,cornerRadii : CGSize, shadowColor:UIColor,shadowOpacity:Float,shadowRadius:CGFloat,shadowOffset:CGSize){
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: cornerRadii)
    layer.masksToBounds = false
  
    let frameLayer = CAShapeLayer()
    frameLayer.path = path.cgPath
    frameLayer.shadowPath = path.cgPath

    frameLayer.lineWidth = 1
    frameLayer.strokeColor = RRSTokens.colorGrey10.cgColor
    frameLayer.fillColor = backgroundColor?.cgColor
    frameLayer.shadowOffset = shadowOffset
    frameLayer.shadowOpacity = shadowOpacity
    frameLayer.shadowRadius = shadowRadius
    frameLayer.shadowColor = shadowColor.cgColor

    layer.mask = frameLayer
    layer.insertSublayer(frameLayer, at: 0)
}

But the result is not expected, there is a black border line visible at the top of tab bar.但结果出乎意料,标签栏顶部有一条黑色边框线可见。 I have tried multiple attempts to play around layer properties to remove that black line but no luck.我已经尝试过多次尝试围绕图层属性来删除那条黑线,但没有运气。 This is what it looks like:这是它的样子:

标签栏顶部可见黑线

Your code is perfect just add below line to your code:您的代码是完美的,只需在您的代码中添加以下行:

self.tabBar.barStyle = .blackOpaque

Hope it'll solve your problem.希望它能解决你的问题。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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