重新加载 UIButton 以更改自定义徽章
Reload UIButton for changing custom badge
我创建了一个带有徽章的自定义 UIButton
,我设置当徽章编号为 0 时,红色圆圈应该消失。
open class ButtonWithBadge: UIButton {
open func addBadgeIcon(badgeNumber: Int, color: UIColor) {
let sizeThatFits = self.sizeThatFits(CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude))
let badgeCount = UILabel(frame: CGRect(x: sizeThatFits.width - 15, y: -07, width: 18, height: 18))
badgeCount.layer.borderColor = UIColor.clear.cgColor
badgeCount.layer.borderWidth = 2
badgeCount.layer.cornerRadius = badgeCount.bounds.size.height / 2
badgeCount.textAlignment = .center
badgeCount.layer.masksToBounds = true
badgeCount.textColor = .white
badgeCount.font = badgeCount.font.withSize(9)
badgeCount.backgroundColor = badgeNumber == 0 ? .clear : color
badgeCount.text = badgeNumber == 0 ? "" : "\(badgeNumber)"
addSubview(badgeCount)
}
我调用这个函数来改变徽章号码。当数字越来越小时,此函数可以完美运行,但是当我将 0 传递给它时,它无法制作背景徽章的 clear
并将其文本替换为 " "
。它仅在您再次打开页面时有效,然后当徽章编号为 0 时,它不会添加该徽章计数视图。
我应该以某种方式在按钮的编号为 0 时重新加载按钮以删除该徽章视图。
谁能帮我解决这个问题。
谢谢
试试下面的代码:
open class ButtonWithBadge: UIButton {
open func addBadgeIcon(badgeNumber: Int, color: UIColor) {
let sizeThatFits = self.sizeThatFits(CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude))
let badgeCount = UILabel(frame: CGRect(x: sizeThatFits.width - 15, y: -07, width: 18, height: 18))
badgeCount.layer.borderColor = UIColor.clear.cgColor
badgeCount.layer.borderWidth = 2
badgeCount.layer.cornerRadius = badgeCount.bounds.size.height / 2
badgeCount.textAlignment = .center
badgeCount.layer.masksToBounds = true
badgeCount.textColor = .white
badgeCount.font = badgeCount.font.withSize(9)
badgeCount.backgroundColor = badgeNumber == 0 ? .clear : color
badgeCount.text = badgeNumber == 0 ? "" : "\(badgeNumber)"
if(badgeNumber != 0){
addSubview(badgeCount)
}
}
我创建了一个带有徽章的自定义 UIButton
,我设置当徽章编号为 0 时,红色圆圈应该消失。
open class ButtonWithBadge: UIButton {
open func addBadgeIcon(badgeNumber: Int, color: UIColor) {
let sizeThatFits = self.sizeThatFits(CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude))
let badgeCount = UILabel(frame: CGRect(x: sizeThatFits.width - 15, y: -07, width: 18, height: 18))
badgeCount.layer.borderColor = UIColor.clear.cgColor
badgeCount.layer.borderWidth = 2
badgeCount.layer.cornerRadius = badgeCount.bounds.size.height / 2
badgeCount.textAlignment = .center
badgeCount.layer.masksToBounds = true
badgeCount.textColor = .white
badgeCount.font = badgeCount.font.withSize(9)
badgeCount.backgroundColor = badgeNumber == 0 ? .clear : color
badgeCount.text = badgeNumber == 0 ? "" : "\(badgeNumber)"
addSubview(badgeCount)
}
我调用这个函数来改变徽章号码。当数字越来越小时,此函数可以完美运行,但是当我将 0 传递给它时,它无法制作背景徽章的 clear
并将其文本替换为 " "
。它仅在您再次打开页面时有效,然后当徽章编号为 0 时,它不会添加该徽章计数视图。
我应该以某种方式在按钮的编号为 0 时重新加载按钮以删除该徽章视图。
谁能帮我解决这个问题。
谢谢
试试下面的代码:
open class ButtonWithBadge: UIButton {
open func addBadgeIcon(badgeNumber: Int, color: UIColor) {
let sizeThatFits = self.sizeThatFits(CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude))
let badgeCount = UILabel(frame: CGRect(x: sizeThatFits.width - 15, y: -07, width: 18, height: 18))
badgeCount.layer.borderColor = UIColor.clear.cgColor
badgeCount.layer.borderWidth = 2
badgeCount.layer.cornerRadius = badgeCount.bounds.size.height / 2
badgeCount.textAlignment = .center
badgeCount.layer.masksToBounds = true
badgeCount.textColor = .white
badgeCount.font = badgeCount.font.withSize(9)
badgeCount.backgroundColor = badgeNumber == 0 ? .clear : color
badgeCount.text = badgeNumber == 0 ? "" : "\(badgeNumber)"
if(badgeNumber != 0){
addSubview(badgeCount)
}
}