gpt4 book ai didi

ios - 在 ios swift 上购物添加到购物车图标角标(Badge)通知

转载 作者:行者123 更新时间:2023-11-28 11:55:44 29 4
gpt4 key购买 nike

如何在 ios swift 的 UIBarButton 上添加角标(Badge)通知,

这是我在 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)
}

角标(Badge)按钮文件

 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")
}
}

关于ios - 在 ios swift 上购物添加到购物车图标角标(Badge)通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060036/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com