gpt4 book ai didi

ios - 如何在 swift 4 中将角标(Badge)放在 UIBarButtonItem 上?

转载 作者:行者123 更新时间:2023-11-28 10:36:41 24 4
gpt4 key购买 nike

我想在 UIBarButtonItem 上放置角标(Badge)。为此,我使用以下引用

Add badge alert in right bar button item in swift

在此,我创建了“UIBarButtonItem+Badge.swift”文件并将该代码放入其中。在我的 View Controller 中,我使用了 UIBarButtonItem 的导出。并调用该函数,但它对我不起作用。我的 View Controller 文件是这个

我的 UIBarButtonItem+Badge.swift 文件是

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

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: [NSAttributedString.Key.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 = CATextLayerAlignmentMode.center
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()
}
}

我的viewcontroller文件是这个

import UIKit

@IBOutlet weak var notificationLabel: UIBarButtonItem!

查看didload函数

notificationLabel.addBadge(text: "4")

最佳答案

这是@VishalPethani 的 swift 4 解决方案,有一些方便的小改动。

将此 UIBarButtonItem 添加到您的代码中:

class BadgedButtonItem: UIBarButtonItem {

public func setBadge(with value: Int) {
self.badgeValue = value
}

private var badgeValue: Int? {
didSet {
if let value = badgeValue,
value > 0 {
lblBadge.isHidden = false
lblBadge.text = "\(value)"
} else {
lblBadge.isHidden = true
}
}
}

var tapAction: (() -> Void)?

private let filterBtn = UIButton()
private let lblBadge = UILabel()

override init() {
super.init()
setup()
}

init(with image: UIImage?) {
super.init()
setup(image: image)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

private func setup(image: UIImage? = nil) {

self.filterBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.filterBtn.adjustsImageWhenHighlighted = false
self.filterBtn.setImage(image, for: .normal)
self.filterBtn.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)

self.lblBadge.frame = CGRect(x: 20, y: 0, width: 15, height: 15)
self.lblBadge.backgroundColor = .red
self.lblBadge.clipsToBounds = true
self.lblBadge.layer.cornerRadius = 7
self.lblBadge.textColor = UIColor.white
self.lblBadge.font = UIFont.systemFont(ofSize: 10)
self.lblBadge.textAlignment = .center
self.lblBadge.isHidden = true
self.lblBadge.minimumScaleFactor = 0.1
self.lblBadge.adjustsFontSizeToFitWidth = true
self.filterBtn.addSubview(lblBadge)
self.customView = filterBtn
}

@objc func buttonPressed() {
if let action = tapAction {
action()
}
}

}

然后你就可以像这样使用它了:

class ViewController: UIViewController {

let btn = BadgedButtonItem(with: UIImage(named: "your_image"))

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.rightBarButtonItem = btn

btn.tapAction = {
self.btn.setBadge(with: 1)
}

}
}

这是一个带有一些自定义的存储库 https://github.com/Syngmaster/BadgedBarButtonItem

关于ios - 如何在 swift 4 中将角标(Badge)放在 UIBarButtonItem 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53335222/

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