首页 > 解决方案 > 快速在右栏按钮项中添加徽章警报

问题描述

我在右栏按钮位置放置了一个简单的按钮。这个按钮显示我的购物车。现在我正在尝试在该按钮上放置一个徽章,该按钮将显示我的购物车项目数。我正在尝试使用 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

这是我的购物车按钮之前的显示方式 在此处输入图像描述

这在右栏按钮代码之后 在此处输入图像描述

标签: iosswift

解决方案


创建名为 '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)

推荐阅读