我正在尝试为我的UIBarButtonItem添加徽章,为此我找到了一个这样的github:
MIBadgeButton - 斯威夫特
但我不知道如何使用它.
这是我的自定义UIBarButtonItem的代码:
let shopingCartBTN = UIButton(type: UIButtonType.Custom) shopingCartBTN.setImage(UIImage(named: "shopingCarBarIcon"), forState: UIControlState.Normal) shopingCartBTN.imageView?.image = UIImage(named: "shopingCarBarIcon") shopingCartBTN.frame = CGRectMake(0, 0, 60, 30)//Just increase the width of button shopingCartBTN.setTitle("5", forState: .Normal) shopingCartBTN.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside) let customBarItem = UIBarButtonItem(customView: shopingCartBTN) self.navigationItem.leftBarButtonItem = customBarItem;
我如何使用MIBadgeButton-Swift为我的UIBarButtonItem制作徽章?
以下是自定义视图和故事板的两个示例
通过设置自定义类从故事板:
@IBOutlet var btnRightBadge: MIBadgeButton! override func viewDidLoad() { super.viewDidLoad() //Custom let badgeButton : MIBadgeButton = MIBadgeButton(frame: CGRectMake(0, 0, 40, 40)) badgeButton.setTitle("T1", forState: UIControlState.Normal) badgeButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) badgeButton.badgeString = "1"; let barButton : UIBarButtonItem = UIBarButtonItem(customView: badgeButton) self.navigationItem.leftBarButtonItem = barButton //From Storyboard btnRightBadge.badgeString = "5" }
您还可以使用ENMBadgedBarButtonItem-Swift