- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 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/
我们可以使用GCM通知作为APNS徽章图标在iPhone应用程序上显示徽章编号吗 由于我无法使用GCM进行设置。 我推荐this link这说 Parameter Platform
注册推送通知时,我没有启用角标(Badge)警报... - (void)registerForPushNotifications { UIRemoteNotificationType noti
我向用户推送了一个没有角标(Badge)键或角标(Badge) = 0 的通知负载,通知无法从通知中心删除。这是我的有效负载: { "aps" : {"alert" : "bala bala
我想在用户每次打开应用程序时清除应用程序图标角标(Badge)。在我的应用程序中,我使用以下代码清除角标(Badge): [UIApplication sharedApplication].appli
在第二版中我可以使用 badge badge-important 我发现 .badge 元素不再具有上下文(-success、-primary 等)类。 如何在版本 3 中实现同样的效果? 例如。我希
Yii2 Twitter Bootstrap 徽章 颜色不变。 In browser source i cant find badge-success, badge-danger e.t.cclass
我目前正在设计一个教育网站。 为此,我们需要根据用户的事件(例如stackoverflow)为他们分配一些徽章。 我正在为此寻找框架。任何人都有任何想法,如何做到这一点? table 设计等 我可以想
我正在制作一个应用程序,该程序显示自从两个人建立关系以来的天数。我想在应用程序图标徽章上显示天数。我知道一旦用户离开应用程序该怎么做,但是我想每天更新图标徽章,即使该应用程序未在后台打开或在后台运行也
我是 iPhone开发的新手。我只是创建像聊天应用程序之类的应用程序。很简单,我使用JSON解析方法,并通过PHP从服务器调用数据并将其发送回给他们。 所以我有一个UIButton我想在此上设置徽章U
我想在控制SplitView中弹出框的UIBarButtonItem上添加徽章 这段代码什么都不做: - (void)splitViewController: (UISplitViewControll
也许这是一个简单的问题... 我有一个带有3个ViewController的3个选项卡栏项的UITabBarController。 我能够将Badge(通过使用setBadgeValue)添加到选项卡
我正在开发 react-native iOS 中的聊天应用程序。我想在收到通知、应用程序被杀死或用户强制退出时在主应用程序图标上进行标记增量。当应用程序处于后台模式时,它运行良好。但是 didRece
当我收到消息时,我会将用户名和未读消息计数写入 CoreData。并借助此功能: func unreadMessagesCountBadge(cell: onlineUserCell, forCoun
这是我的代码,我将日期放在应用程序角标(Badge)中。我想知道如何每天重新加载这个号码而不进入应用程序重新加载它。提前致谢! NSDate *today = [NSDate date]; NSCal
我有一个带有标签栏 Controller 的应用程序。这些 View 之一是 TableView 。有一种方法可以在标签栏中设置此 View 的角标(Badge)。这有效......但只有当用户触摸此
在我的数据库中,我将传递的消息计数存储在一个表字段中。对于我发送到特定设备的每条消息,我都会增加这个值。 当设备收到带有角标(Badge)设置的 payloa 时,它会在应用程序图标上显示红色圆圈。
我的应用程序包含许多嵌入式练习,这些练习会在应用程序使用期间显示给用户。我计划发布我的应用程序的定期更新,其中包含额外的练习。每次(标准应用程序商店应用程序)更新后,我想在应用程序图标上添加一个角标(
我试图了解当用户的网络连接特别差或没有网络连接时,iOS 上远程通知的最佳实践。 场景是这样的: 用户会收到一些远程通知,并在将来的某个时候打开应用程序。此时,我不想让服务器尽快知道角标(Badge)
我有一个函数,可以在单元格上添加角标(Badge): unreadMessagesCountBadge(cell, forCount: messagesCount) 但我不知道,如何检测我需要添加此角
我的问题的根源是我无法在真实设备上测试推送消息,因为这个问题似乎是 Xcode bug,但这不是现在的主题。我只是添加这个来回答为什么我不能正常测试我的实现。 因此,我使用 OneSignal SDK
我是一名优秀的程序员,十分优秀!