如何解决将顶部边框添加到选定的TabBar
如何在选定的TabBar的顶部添加边框。 下面是我曾经在底部添加但我想要的方法
extension UIImage {
func createSelectionIndicator(color: UIColor,size: CGSize,lineWidth: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size,false,0)
color.setFill()
UIRectFill(CGRect(x: 0,y: size.height - lineWidth,width: size.width,height: lineWidth))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: BLUE,size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count),height: tabBar.frame.height),lineWidth: 2.0)
解决方法
您可以使用该功能在任意一侧创建边框-
enum Side: String {
case top,left,right,bottom
}
extension UIImage {
func createSelectionIndicator(color: UIColor,size: CGSize,lineThickness: CGFloat,side: Side) -> UIImage {
var xPosition = 0.0
var yPosition = 0.0
var imgWidth = 2.0
var imgHeight = 2.0
switch side {
case .top:
xPosition = 0.0
yPosition = 0.0
imgWidth = size.width
imgHeight = lineThickness
case .bottom:
xPosition = 0.0
yPosition = size.height - lineThickness
imgWidth = size.width
imgHeight = lineThickness
case .left:
xPosition = 0.0
yPosition = 0.0
imgWidth = lineThickness
imgHeight = size.height
case .right:
xPosition = size.width - lineThickness
yPosition = 0.0
imgWidth = lineThickness
imgHeight = size.height
}
UIGraphicsBeginImageContextWithOptions(size,false,0)
color.setFill()
UIRectFill(CGRect(x: xPosition,y: yPosition,width: imgWidth,height: imgHeight))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: BLUE,size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count),height: tabBar.frame.height),lineThickness: 2.0,side: .top)
让我知道这是否有用,或者您需要其他帮助。 快乐编码:)
,导入UIKit
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.tabBar.addBorder(.top,color: .red,thickness: 2.0)
}
}
extension UITabBar {
func addBorder(_ edge: UIRectEdge,color: UIColor,thickness: CGFloat) {
let subview = UIView()
subview.translatesAutoresizingMaskIntoConstraints = false
subview.backgroundColor = color
self.addSubview(subview)
switch edge {
case .top,.bottom:
subview.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
subview.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 0).isActive = true
subview.heightAnchor.constraint(equalToConstant: thickness).isActive = true
if edge == .top {
subview.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
} else {
subview.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
}
case .left,.right:
subview.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
subview.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
subview.widthAnchor.constraint(equalToConstant: thickness).isActive = true
if edge == .left {
subview.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
} else {
subview.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 0).isActive = true
}
default:
break
}
}
}
tabBarController?.tabBar.addBorder(.top,color: .orange,thickness: 5.0)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。