最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift给已有的自己定制的button添加badge

iOS crifan 913浏览 0评论

之前已经实现了自己定制扩展的button

现在希望给其中添加badge

经过一番折腾,最终用代码实现badge如下:

<code>//
//  CreateNewButton.swift
//  SalesApp
//
//  Created by licrifan on 16/5/23.
//  Copyright © 2016年 licrifan. All rights reserved.
//

import UIKit
import Cartography

class CreateNewButton: UIButton {
    var newImageView:UIImageView
    var newLabel:UILabel
    var badgeView:UIView

    var badgeInt:Int {
        didSet {
            self.updateBadgeView()
        }
    }

    init(newImage:UIImage, newTitle:String, badgeInt:Int = 0) {
        gLog.info("newImage=\(newImage), newTitle=\(newTitle), badgeInt=\(badgeInt)")

        self.newImageView = UIImageView()
        self.newLabel = UILabel()
        self.badgeView = UIView()
        self.badgeInt = badgeInt

        super.init(frame: CGRectZero)

        //1. image view
        self.addSubview(self.newImageView)
        self.newImageView.image = newImage
        constrain(newImageView) {newImageView in
            newImageView.top == newImageView.superview!.top + 12
            newImageView.height == self.newImageView.image!.size.height
            newImageView.centerX == newImageView.superview!.centerX
            newImageView.width &lt;= newImageView.superview!.width
        }

        //2. badge view
        self.updateBadgeView()

        //3. new title label
        self.addSubview(self.newLabel)
        self.newLabel.text = newTitle
        self.newLabel.textAlignment = .Center
        self.newLabel.font = UIFont.systemFontOfSize(11)
        constrain(newLabel, newImageView) {newLabel, newImageView in
            newLabel.top == newImageView.bottom + 10
            newLabel.height == 16
            newLabel.width &lt;= newLabel.superview!.width
            newLabel.centerX == newLabel.superview!.centerX
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func updateBadgeView() {
        gLog.info("badgeInt=\(badgeInt)")

        self.badgeView.removeFromSuperview()

        if badgeInt &gt; 0 {
            self.badgeView.hidden = false
            self.badgeView = drawBadgeView(String(badgeInt))

            //badgeView.frame=(0.0, 0.0, 17.0, 17.0)
            let badgeFrameSize = badgeView.frame.size
            self.addSubview(self.badgeView)
            constrain(self.badgeView) {badgeView in
                badgeView.top == badgeView.superview!.top + 8
                badgeView.left == badgeView.superview!.right - badgeFrameSize.width - 12
            }
        } else {
            self.badgeView = UIView()
            self.badgeView.hidden = true
        }
    }
}



    var toCompleteButton:CreateNewButton


        self.toCompleteButton = CreateNewButton(newImage: UIImage(named: "new_toComplete")!, newTitle: "待完善客户", badgeInt: 6)

        //2.2 new to complete
        toCompleteButton.addTarget(self, action: #selector(self.toCompleteCustomer(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        self.newView.addSubview(toCompleteButton)
        constrain(toCompleteButton, newCustomerButton) {toCompleteButton, newCustomerButton in
            toCompleteButton.top == toCompleteButton.superview!.top
            toCompleteButton.left == newCustomerButton.right
            toCompleteButton.width == NewButtonWidth
            toCompleteButton.bottom == toCompleteButton.superview!.bottom
        }



    func toCompleteCustomer(sender:UIButton){
        gLog.info("")

        self.toCompleteButton.badgeInt += 1
    }
</code>

其中可见:

在初始化之后,可以通过直接设置badge,去刷新对应的badgeView

效果如下:

多次点击,徽章数字增加后:

转载请注明:在路上 » [已解决]swift给已有的自己定制的button添加badge

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.233 seconds, using 18.92MB memory