我想将拐角半径设置为uitabar并设置阴影。它看起来应该像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
}
}
最佳答案
使用下面的UIView扩展方法制作圆形顶角
func roundedTop(_ radius: CGFloat) {
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topRight, .topLeft],
cornerRadii: CGSize(width: radius, height: radius))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
并对shadow使用下面的UIView扩展方法。根据您的要求修改方法。
enum VerticalLocation: String {
case bottom
case top
}
func addShadow(location: VerticalLocation, color: UIColor = .black, opacity: Float = 0.3, radius: CGFloat = 2.0) {
switch location {
case .bottom:
addShadow(offset: CGSize(width: 0, height: 5), color: color, opacity: opacity, radius: radius)
case .top:
addShadow(offset: CGSize(width: 0, height: -5), color: color, opacity: opacity, radius: radius)
}
}
func addShadow(offset: CGSize, color: UIColor = .black, opacity: Float = 0.3, radius: CGFloat = 2.0) {
self.layer.masksToBounds = false
self.layer.shadowColor = color.cgColor
self.layer.shadowOffset = offset
self.layer.shadowOpacity = opacity
self.layer.shadowRadius = radius
}
希望能帮上忙。
关于swift - 如何在UITabBar的顶部创建圆角,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56145974/