0

I created a badge over bar button like this in order to show no of items in cart.

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

Inside View Controller :

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

Using badge function :

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

But after adding this badge the bar button item becomes untapable. Control not going inside of the button action method. What should be done here in order to make button tapable.

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
  • In `addBadge`, give the button a width constraint and a height constraint. See if that helps. – matt Oct 18 '18 at 13:26

2 Answers2

0

Please check UITapGestureRecognizer on your parent view of the view controller. if exists removed the tap gesture, this make UIBarButtonItems began to respond properly to all selectors.

user1376400
  • 614
  • 1
  • 4
  • 8
0

I was not able to judge whats the issue with the code of SSBadgeButton. Later I figured out that what I want to achieve (placing UILabel with UIBarButtonItem) can be achieved through storyboard.

Solution is : U can drag UIView inside ToolBar and can place UiLabel & Button inside of that view. Referred from this answer

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37