swift - 如何在 UITabBar 顶部创建圆角
问题描述
我想将角半径设置为 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
}
}
解决方案
- 添加扩展:
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
}
}
调用分机:
tabBar.makeCornerTabBar(shadowColor: UIColor.lightGray, fillColor: UIColor.white, opacity: 1, corners: [.topLeft, .topRight], offset: CGSize(width: 0, height: 5), radius: 25)
推荐阅读
- javascript - 输入时是否可以使文本框垂直滚动?
- gstreamer - prelink-rtld:找不到命令
- python - QSyntaxHighlighter 显示问题
- ruby-on-rails - 提供重构代码(过滤方法)
- python - 如何解决:“无法导入名称:OneCycleLR”?
- c# - C# 交互式与即时窗口。差异、目的和用例
- javascript - Javascript 本机 .remove() 不适用于 HTMLCollection。Javascript错误?
- ionic-framework - Ionic V4 中的深度链接?
- twilio - Twilio 功能可将未接电话或在办公时间以外收到的电话转移到语音信箱,否则拨打 sip
- scala - Scala ZIO Ref 数据类型