Swift 实现一个简单的带标题的按钮

7 篇文章 0 订阅

由于项目需要,需要实现一个带图标的按钮,而自带的的 UIButton 并不能很好的满足需要,所以就自己实现一个,可以实现自定义标题位置,可以指定标题位置相对于图标的位置,支持上、下、左、右。

代码如下:

//
//  Titleself.iconButton.swift
//  Genyida
//
//  Created by mac min on 2021/8/19.
//  Copyright © 2021 huangzhengguo. All rights reserved.
//

import Foundation

enum TitlePosition: Int8 {
    case Top = 0
    case Right = 1
    case Bottom = 2
    case Left = 3
}

class TitleButton: UIView {
    
    var titleLabel = UILabel(frame: CGRect.zero)
    var iconButton = UIButton(frame: CGRect.zero)
    var clickCallback: ((Bool) -> Void)?

    init(frame: CGRect, image: String, selectedImage: String, title: String, selectedTitle: String, selected: Bool, titlePosition: TitlePosition = .Bottom, textColor: UIColor = .white) {
        super.init(frame: frame)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        self.layer.cornerRadius = GlobalConstant.DEFAULT_VIEW_CORNER_RADIUS
        self.backgroundColor = .clear
        self.layer.borderWidth = 1.0
        self.layer.borderColor = UIColor.clear.cgColor
        self.isUserInteractionEnabled = true
        
        let viewTap = UITapGestureRecognizer(target: self, action: #selector(viewTapAction))

        self.addGestureRecognizer(viewTap)

        self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
        self.titleLabel.textColor = textColor

        self.iconButton.tag = 10001
        self.iconButton.tintColor = .lightGray
        self.iconButton.isUserInteractionEnabled = false
        self.iconButton.translatesAutoresizingMaskIntoConstraints = false
        self.iconButton.setImage(UIImage(named: image), for: .normal)
        self.iconButton.setImage(UIImage(named: selectedImage), for: .selected)
        
        self.addSubview(self.titleLabel)
        self.addSubview(self.iconButton)
        
        self.updateViews(selected: selected, title: title, selectedTitle: selectedTitle)
        
        let labelLeading = NSLayoutConstraint(item: self.titleLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)
        let labelTrailing = NSLayoutConstraint(item: self.titleLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0.0)
        let labelTop = NSLayoutConstraint(item: self.titleLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 3.0)
        let labelBottom = NSLayoutConstraint(item: self.titleLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0)
        let labelWidth = NSLayoutConstraint(item: self.titleLabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0.5, constant: 0.0)
        let labelHeight = NSLayoutConstraint(item: self.titleLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 21.0)
        
        let buttonLeading = NSLayoutConstraint(item: self.iconButton, attribute: .leading, relatedBy: .equal, toItem: self.titleLabel, attribute: .trailing, multiplier: 1.0, constant: 3.0)
        let buttonTrailing = NSLayoutConstraint(item: self.iconButton, attribute: .trailing, relatedBy: .equal, toItem: self.titleLabel, attribute: .leading, multiplier: 1.0, constant: -8.0)
        var buttonTop = NSLayoutConstraint(item: self.iconButton, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 3.0)
        var buttonBottom = NSLayoutConstraint(item: self.iconButton, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -3.0)
        let buttonWidthHeight = NSLayoutConstraint(item: self.iconButton, attribute: .width, relatedBy: .equal, toItem: self.iconButton, attribute: .height, multiplier: 1.0, constant: 0.0)
        let buttonCenterX = NSLayoutConstraint(item: self.iconButton, attribute: .centerX, relatedBy: .equal, toItem: self.titleLabel, attribute: .centerX, multiplier: 1.0, constant: 0.0)
        
        if titlePosition == .Top {
            // 顶部标题
            self.titleLabel.textAlignment = .center
            buttonTop = NSLayoutConstraint(item: self.iconButton, attribute: .top, relatedBy: .equal, toItem: self.titleLabel, attribute: .bottom, multiplier: 1.0, constant: 3.0)
            
            self.addConstraints([labelLeading, labelTop, labelTrailing, labelHeight])
            self.addConstraints([buttonCenterX, buttonTop, buttonBottom, buttonWidthHeight])
        } else if titlePosition == .Bottom {
            // 底部标题
            self.titleLabel.textAlignment = .center
            buttonBottom = NSLayoutConstraint(item: self.iconButton, attribute: .bottom, relatedBy: .equal, toItem: self.titleLabel, attribute: .top, multiplier: 1.0, constant: -3.0)
            
            self.addConstraints([labelLeading, labelBottom, labelTrailing, labelHeight])
            self.addConstraints([buttonCenterX, buttonTop, buttonBottom, buttonWidthHeight])
        } else if titlePosition == .Right {
            // 右部标题
            self.titleLabel.textAlignment = .left
            self.addConstraints([labelTrailing, labelTop, labelBottom, labelWidth])
            self.addConstraints([buttonTrailing, buttonTop, buttonBottom, buttonWidthHeight])
        } else {
            // 左部标题
            self.titleLabel.textAlignment = .right
            self.addConstraints([labelLeading, labelTop, labelBottom, labelWidth])
            self.addConstraints([buttonLeading, buttonTop, buttonBottom, buttonWidthHeight])
        }
    }
    
    func updateViews(selected: Bool, title: String, selectedTitle: String) -> Void {
        if selected == true {
            self.titleLabel.text = selectedTitle
        } else {
            self.titleLabel.text = title
        }
        
        self.iconButton.isSelected = selected
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func viewTapAction(sender: UITapGestureRecognizer) -> Void {
        let view = sender.view
        
        let btn: UIButton = view?.viewWithTag(10001) as! UIButton
        
        btn.isSelected = !btn.isSelected
        
        self.clickCallback?(btn.isSelected)
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值