ios swift 上的购物添加到购物车图标徽章通知

Shopping Add to cart icon badge notification on ios swift

如何在 ios swift、

中的 UIBarButton 上添加徽章通知

这是我在 swift

中的 UIBarButton 代码
let addtocartButton = UIBarButtonItem(image: UIImage(named: "Addtocartimg"),
                                      style: .plain,
                                     target: self,
                                     action: #selector(self.movToToCarts))

self.navigationItem.rightBarButtonItem = addtocartButton
let cartButton = SSBadgeButton()
override func viewDidLoad() {
    super.viewDidLoad()

    cartButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
    cartButton.setImage(UIImage(named: "menu")?.withRenderingMode(.alwaysTemplate), for: .normal)
    cartButton.badgeEdgeInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 15)
    cartButton.addTarget(self, action: #selector(cartButtonTapped), for: .touchUpInside)
    cartButton.badge = "4"

    self.navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: cartButton)
}

徽章按钮文件

 import Foundation
 import UIKit

 class SSBadgeButton: UIButton {

 var badgeLabel = UILabel()

 var badge: String? {
    didSet {
        addBadgeToButon(badge: badge)
    }
 }

public var badgeBackgroundColor = UIColor.red {
    didSet {
        badgeLabel.backgroundColor = badgeBackgroundColor
    }
}

public var badgeTextColor = UIColor.white {
    didSet {
        badgeLabel.textColor = badgeTextColor
    }
}

public var badgeFont = UIFont.systemFont(ofSize: 12.0) {
    didSet {
        badgeLabel.font = badgeFont
    }
}

public var badgeEdgeInsets: UIEdgeInsets? {
    didSet {
        addBadgeToButon(badge: badge)
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    addBadgeToButon(badge: nil)
}

func addBadgeToButon(badge: String?) {
    badgeLabel.text = badge
    badgeLabel.textColor = badgeTextColor
    badgeLabel.backgroundColor = badgeBackgroundColor
    badgeLabel.font = badgeFont
    badgeLabel.sizeToFit()
    badgeLabel.textAlignment = .center
    let badgeSize = badgeLabel.frame.size

    let height = max(18, Double(badgeSize.height) + 5.0)
    let width = max(height, Double(badgeSize.width) + 10.0)

    var vertical: Double?, horizontal: Double?
    if let badgeInset = self.badgeEdgeInsets {
        vertical = Double(badgeInset.top) - Double(badgeInset.bottom)
        horizontal = Double(badgeInset.left) - Double(badgeInset.right)

        let x = (Double(bounds.size.width) - 10 + horizontal!)
        let y = -(Double(badgeSize.height) / 2) - 10 + vertical!
        badgeLabel.frame = CGRect(x: x, y: y, width: width, height: height)
    } else {
        let x = self.frame.width - CGFloat((width / 2.0))
        let y = CGFloat(-(height / 2.0))
        badgeLabel.frame = CGRect(x: x, y: y, width: CGFloat(width), height: CGFloat(height))
    }

    badgeLabel.layer.cornerRadius = badgeLabel.frame.height/2
    badgeLabel.layer.masksToBounds = true
    addSubview(badgeLabel)
    badgeLabel.isHidden = badge != nil ? false : true
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.addBadgeToButon(badge: nil)
    fatalError("init(coder:) has not been implemented")
}
}

你可以在this question. Recommend you to reference QMUIBadge的代码下看到我的回答。但它在 Objective-C.