ios - 快速在右侧栏按钮项目中添加角标(Badge)警报

标签 ios swift

我在右栏按钮位置放置了一个简单的按钮。这个按钮显示我的购物车。现在我正在尝试在该按钮上放置一个角标(Badge),它将显示我的购物车商品数量。我正在尝试使用 FontAwesome pods。我已经放置了一个右栏按钮项目,但是当我运行它时,它用购物车按钮替换了计数的右栏按钮。我希望它出现在购物车按钮上并显示数量。我怎样才能得到它? 这就是我要在右 bat 按钮上放置角标(Badge)的原因

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

这是我的购物车按钮之前的显示方式 enter image description here

这个是看右栏按钮的代码 enter image description here

最佳答案

创建名为“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 中添加角标(Badge),如下所示:

let rightBarButton = self.navigationItem.rightBarButtonItem

rightBarButton?.addBadge(number: 4)

关于ios - 快速在右侧栏按钮项目中添加角标(Badge)警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50329779/

相关文章:

ios - ARKit 代码问题 {unknown error -1=ffffffffffffffff 错误 : Task failed with exit 1}

swift - var = str 创建 'ambiguous use of appendingPathComponent' 错误

Swift for in loop : use var get warning to use let, use let get 错误

ios - 如何以编程方式在 TableViewCell 中设置固定 ImageView 大小?

ios - 有人遇到过以全屏模式 iOS/Mobile Safari 运行的 Web 应用程序的缓存问题吗?

ios - 重新打开应用程序时出现关系故障

ios - 字符串过滤器的核心数据数组

ios - swift : How to set two action on Single Gesture

arrays - Swift 的 filter 方法稳定吗?

ios - 在顶级 View 中居中 SwiftUI View