gpt4 book ai didi

swift - 如何在 UITabBar 顶部创建圆角

转载 作者:行者123 更新时间:2023-11-28 07:27:16 26 4
gpt4 key购买 nike

我想将圆角半径设置为 UITabBar 并设置阴影。它应该看起来像 img1 , 但它看起来像 img2 .

我的代码:

  tabBar.barTintColor = .white
tabBar.isTranslucent = false

tabBar.dropShadow(shadowColor: UIColor.lightGray, fillColor: UIColor.white, opacity: 1, offset: CGSize(width: 0, height: 5), radius: 25)

tabBar.layer.masksToBounds = false
tabBar.isTranslucent = true
tabBar.barStyle = .blackOpaque
tabBar.layer.cornerRadius = 13
tabBar.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

和扩展

extension UIView{
func dropShadow(shadowColor: UIColor = UIColor.black,
fillColor: UIColor = UIColor.white,
opacity: Float = 0.2,
offset: CGSize = CGSize(width: 0.0, height: 5.0),
radius: CGFloat = 10) -> CAShapeLayer {

let shadowLayer = CAShapeLayer()

shadowLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: radius).cgPath
shadowLayer.fillColor = fillColor.cgColor
shadowLayer.shadowColor = shadowColor.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = offset
shadowLayer.shadowOpacity = opacity
shadowLayer.shadowRadius = radius
layer.insertSublayer(shadowLayer, at: 0)
return shadowLayer
}
}

最佳答案

  1. 添加扩展:
        extension UIView {
func makeCornerTabBar(shadowColor: UIColor = UIColor.black,
fillColor: UIColor = UIColor.white,
opacity: Float = 0.2,
corners: UIRectCorner,
offset: CGSize = CGSize(width: 0.0, height: 5.0),
radius: CGFloat = 0.0) -> CAShapeLayer {

let shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
shadowLayer.fillColor = fillColor.cgColor
shadowLayer.shadowColor = shadowColor.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = offset
shadowLayer.shadowOpacity = opacity
layer.mask = shadowLayer
layer.insertSublayer(shadowLayer, at: 0)
return shadowLayer
}
}
  1. 调用扩展:

    tabBar.makeCornerTabBar(shadowColor: UIColor.lightGray, fillColor: UIColor.white, opacity: 1, corners: [.topLeft, .topRight], offset: CGSize(width: 0, height: 5), radius: 25)

关于swift - 如何在 UITabBar 顶部创建圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com