How to add Badges on UIBarbutton item?

前端 未结 10 1691
广开言路
广开言路 2020-11-28 21:29

Hi friends am new to iphone developing. Am struggle with add badge values on UIBarbutton item on right side. I have tried but i can\'t solve this problem. Can anyone help me

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 21:40

    Here is a simple Swift 4 solution for it (with some customisation) https://github.com/Syngmaster/BadgedBarButtonItem

    Just drag and drop the class into your project and you can use it like that:

    class ViewController: UIViewController {
    
        let btn = BadgedButtonItem(with: UIImage(named: "your_image"))
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            btn.badgeTextColor = .black
            btn.badgeTintColor = .yellow
            btn.position = .right
            btn.hasBorder = true
            btn.borderColor = .red
            btn.badgeSize = .medium
            btn.badgeAnimation = true
    
            self.navigationItem.rightBarButtonItem = btn
    
            btn.tapAction = {
                self.btn.setBadge(with: 4)
            }
    
        }
    
    }
    

提交回复
热议问题