首页 > 解决方案 > 在 UIButton 上结合扩展和自定义类会产生问题

问题描述

我有一个类和一个扩展名UIButton。删除标题并在单击 ( LoadingButton) 时显示加载指示器的自定义类。- 声明自定义函数以将渐变应用于按钮等的扩展。

两者都在工作,但typeMain()在类按钮上调用该函数LoadingButton涉及一个问题 - 按钮的标题没有被删除,因此加载按钮显示在标题的顶部。

如何将两者结合起来解决这个问题?

顺便说一句:button.typeMain()正在被调用viewDidLoad()

加载按钮:

class LoadingButton: UIButton {
var originalButtonText: String?
var activityIndicator: UIActivityIndicatorView!

func showLoading() {
    originalButtonText = self.titleLabel?.text
    self.setTitle("", for: .normal)

    if (activityIndicator == nil) {
        activityIndicator = createActivityIndicator()
    }

    showSpinning()
}

func hideLoading() {
    self.setTitle(originalButtonText, for: .normal)
    activityIndicator.stopAnimating()
}

private func createActivityIndicator() -> UIActivityIndicatorView {
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.hidesWhenStopped = true
    activityIndicator.color = .white
    return activityIndicator
}

private func showSpinning() {
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(activityIndicator)
    centerActivityIndicatorInButton()
    activityIndicator.startAnimating()
}

private func centerActivityIndicatorInButton() {
    let xCenterConstraint = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: activityIndicator, attribute: .centerX, multiplier: 1, constant: 0)
    self.addConstraint(xCenterConstraint)

    let yCenterConstraint = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: activityIndicator, attribute: .centerY, multiplier: 1, constant: 0)
    self.addConstraint(yCenterConstraint)
}

自定义每个按钮的扩展:

extension UIButton {

func typeMain() {
    self.translatesAutoresizingMaskIntoConstraints = false
    let height = UIScreen.main.bounds.height * 0.07
    let width = UIScreen.main.bounds.width * 0.9
    self.heightAnchor.constraint(equalToConstant: height).isActive = true
    self.widthAnchor.constraint(equalToConstant: width).isActive = true

    layoutIfNeeded()

    self.addCharacterSpacing()
    self.tintColor = UIColor.white
    let color = UIColor(red: 11/255, green: 95/255, blue: 244/255, alpha: 1)
    let sndColor = UIColor(red: 106/255, green: 178/255, blue: 255/255, alpha: 1)

    self.layer.cornerRadius = self.frame.size.height / 5.0

    self.applyGradient(colours: [color, sndColor], locations: [0.0, 1.0])

    let shadowSize : CGFloat = 2.0
    self.layer.shadowColor = UIColor(red: 106/255, green: 178/255, blue: 255/255, alpha: 1).cgColor
    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    self.layer.shadowOpacity = 0.4
    let shadowPath = UIBezierPath(rect: CGRect(x: -shadowSize / 2,
                                               y: shadowSize,
                                               width: self.frame.size.width + shadowSize,
                                               height: self.frame.size.height + shadowSize))
    self.layer.shadowPath = shadowPath.cgPath
    self.layer.shadowRadius = 5
    self.layer.masksToBounds = false
}

字符间距

extension UILabel {
func addCharacterSpacing(kernValue: Double = 0.5) {
    if let labelText = text, labelText.count > 0 {
        let attributedString = NSMutableAttributedString(string: labelText)
        attributedString.addAttribute(NSAttributedString.Key.kern, value: kernValue, range: NSRange(location: 0, length: attributedString.length - 1))
        attributedText = attributedString
    }
}

标签: iosswift

解决方案


您的自定义类使用按钮标题,扩展方法使用按钮属性标题。更新您的自定义类以保留对属性标题的引用

class LoadingButton: UIButton {
    var originalButtonText: String?
    var attributedButtonText: NSAttributedString?
    var activityIndicator: UIActivityIndicatorView!

    func showLoading() {
        originalButtonText = self.titleLabel?.text
        attributedButtonText = self.attributedTitle(for: .normal)
        self.setTitle("", for: .normal)
        self.setAttributedTitle(NSAttributedString(string: ""), for: .normal)

        if (activityIndicator == nil) {
            activityIndicator = createActivityIndicator()
        }            
        showSpinning()
    }        
    func hideLoading() {
        self.setAttributedTitle(attributedButtonText, for: .normal)
        self.setTitle(originalButtonText, for: .normal)
        activityIndicator.stopAnimating()
    }     
}

推荐阅读