在栏按钮上的徽章使其无法点击后

After badge over bar button makes it untapable

我在这样的栏按钮上创建了一个徽章,以显示购物车中没有商品。

Class SSBadgeButton

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

内部视图控制器:

func addBadge(itemvalue: String) {

    let bagButton = SSBadgeButton()
    bagButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
    bagButton.tintColor = UIColor.darkGray
    bagButton.setImage(UIImage(named: "ShoppingBag")?.withRenderingMode(.alwaysTemplate), for: .normal)
    bagButton.badgeEdgeInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 15)
    bagButton.badge = itemvalue
    bagButton.isUserInteractionEnabled = true
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: bagButton)
}

使用徽章功能:

        self.addBadge(itemvalue: data[0]["total_products_in_cart"].stringValue)

但在添加此徽章后,栏按钮项目变得无法点击。控制不进入按钮操作方法。在这里应该做什么才能使按钮可点击。

请检查视图控制器的父视图上的 UITapGestureRecognizer。如果存在,则移除点击手势,这会使 UIBarButtonItems 开始正确响应所有选择器。

我无法判断 SSBadgeButton 的代码有什么问题。 后来想通了,我想实现的(放置UILabel和UIBarButtonItem)可以通过storyboard来实现。

解决方法是: 您可以将 UIView 拖到 ToolBar 内,并可以将 UiLabel 和 Button 放置在该视图内。 引用自此answer