ios - 快速在右栏按钮项中添加徽章警报
问题描述
我在右栏按钮位置放置了一个简单的按钮。这个按钮显示我的购物车。现在我正在尝试在该按钮上放置一个徽章,该按钮将显示我的购物车项目数。我正在尝试使用 FontAwesome 豆荚。我已经放置了一个右栏按钮项目,但是当我运行它时,它用购物车按钮替换了右栏按钮。我希望它出现在购物车按钮上并显示计数。我怎样才能得到它?这就是我试图在右蝙蝠按钮上放置一个徽章
let rightButton = UIButton(frame: CGRect(x:0,y:0,width:20,height:20))
rightButton.titleLabel?.font = UIFont.fontAwesome(ofSize: 15)
rightButton.setTitle(String.fontAwesomeIcon(name: .circle), for: .normal)
let rightButtonItem : UIBarButtonItem = UIBarButtonItem(customView: rightButton)
rightButton.titleLabel?.font = UIFont.fontAwesome(ofSize: 20)
rightButton.titleLabel?.text = String.fontAwesomeIcon(name: .github)
self.navigationItem.rightBarButtonItem = rightButtonItem
解决方案
创建名为 'UIBarButtonItem+Badge.swift' 的文件并添加以下代码
import UIKit
extension CAShapeLayer {
func drawRoundedRect(rect: CGRect, andColor color: UIColor, filled: Bool) {
fillColor = filled ? color.cgColor : UIColor.white.cgColor
strokeColor = color.cgColor
path = UIBezierPath(roundedRect: rect, cornerRadius: 7).cgPath
}
}
private var handle: UInt8 = 0;
extension UIBarButtonItem {
private var badgeLayer: CAShapeLayer? {
if let b: AnyObject = objc_getAssociatedObject(self, &handle) as AnyObject? {
return b as? CAShapeLayer
} else {
return nil
}
}
func setBadge(text: String?, withOffsetFromTopRight offset: CGPoint = CGPoint.zero, andColor color:UIColor = UIColor.red, andFilled filled: Bool = true, andFontSize fontSize: CGFloat = 11)
{
badgeLayer?.removeFromSuperlayer()
if (text == nil || text == "") {
return
}
addBadge(text: text!, withOffset: offset, andColor: color, andFilled: filled)
}
private func addBadge(text: String, withOffset offset: CGPoint = CGPoint.zero, andColor color: UIColor = UIColor.red, andFilled filled: Bool = true, andFontSize fontSize: CGFloat = 11)
{
guard let view = self.value(forKey: "view") as? UIView else { return }
var font = UIFont.systemFont(ofSize: fontSize)
if #available(iOS 9.0, *) { font = UIFont.monospacedDigitSystemFont(ofSize: fontSize, weight: UIFont.Weight.regular) }
let badgeSize = text.size(withAttributes: [NSAttributedStringKey.font: font])
// Initialize Badge
let badge = CAShapeLayer()
let height = badgeSize.height;
var width = badgeSize.width + 2 /* padding */
//make sure we have at least a circle
if (width < height) {
width = height
}
//x position is offset from right-hand side
let x = view.frame.width - width + offset.x
let badgeFrame = CGRect(origin: CGPoint(x: x, y: offset.y), size: CGSize(width: width, height: height))
badge.drawRoundedRect(rect: badgeFrame, andColor: color, filled: filled)
view.layer.addSublayer(badge)
// Initialiaze Badge's label
let label = CATextLayer()
label.string = text
label.alignmentMode = kCAAlignmentCenter
label.font = font
label.fontSize = font.pointSize
label.frame = badgeFrame
label.foregroundColor = filled ? UIColor.white.cgColor : color.cgColor
label.backgroundColor = UIColor.clear.cgColor
label.contentsScale = UIScreen.main.scale
badge.addSublayer(label)
// Save Badge as UIBarButtonItem property
objc_setAssociatedObject(self, &handle, badge, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
private func removeBadge() {
badgeLayer?.removeFromSuperlayer()
}
}
现在简单地在 UIBarButtonItem 中添加徽章,如下所示:
let rightBarButton = self.navigationItem.rightBarButtonItem
rightBarButton?.addBadge(number: 4)
推荐阅读
- php - Laravel 8 Gate/Policy 无法打开流:Nginx 服务器上的权限被拒绝
- assembly - 关于 Aarch64 汇编地址格式的问题
- bash - 如何解决 mv cannot stat No such file or directory 错误
- flutter - Flutter 没有启动作为 API 响应接收的动态 URL
- corda - 证书颁发机构如何签署 Corda 网络中的证书?
- javascript - 我将如何动态克隆站点并将自定义 css 应用于它?
- java - 将参数从 JSP 页面传递到 servlet 而不使用表单参数
- python - 我有两列分别是 Volume_daily 和 Volume_mean。我无法将 volume_daily 数据与 pandas 中的体积平均值进行比较
- php - 如果通过 PDO 在 mysql 上存在行,如何返回 true 或 false
- rocksdb - RocksDb Java API 对事务的支持