子类uibutton .-将标题文本重置帧更改为原始大小

时间:2018-03-03 13:21:17

标签: uibutton subclassing

我将一个uibutton子类化,并有一些代码可以改变它的宽度/高度。我有另一个功能,只是更改标题文本。当我运行该功能时,该按钮将恢复为原始大小。这是我的代码:

import UIKit

enum ButtonType {
case newGameButton
case showStatsButton
case addTeamButton
case addUserButton
}

class LSS_Button: UIButton {

var myButtonType:ButtonType?
var originalHeight:CGFloat?


override func layoutSubviews() {

    super.layoutSubviews()

    if var titleFrame = self.titleLabel?.frame {
        titleFrame.size.height = self.bounds.size.height;
        titleFrame.origin.y = self.titleEdgeInsets.top + 3.0;

        self.titleLabel?.frame = titleFrame;
    }
}

func toggleSize() {

    var newFrame = self.frame
    var makeRound = false

    if self.frame.size.width != 40.0 {

        newFrame.size.width = 40.0
        newFrame.size.height = 40.0
        makeRound = true

    } else {

        if let newWidth = self.superview?.frame.size.width {
            newFrame.size.width = newWidth
            newFrame.size.height = self.originalHeight!
            makeRound = false
        }

    }

    UIView.animate(withDuration: 0.25, animations: {

        self.frame = newFrame

        if makeRound {
            self.layer.cornerRadius = self.frame.size.width/2
        } else {
            self.layer.cornerRadius = 0.0
        }
    })
}

func editTitle(_ strTitle:String) {
    self.titleLabel?.text = strTitle
}

以下是一些发生的事情的图像:

Original button

Changed size

After editing text

此外,该按钮还原为原始文本。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我是个白痴。自从我编写iOS以来已经有一段时间了。

self.setTitle(strTitle, for: .normal)