我将一个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
}
以下是一些发生的事情的图像:
此外,该按钮还原为原始文本。有办法解决这个问题吗?
答案 0 :(得分:0)
self.setTitle(strTitle, for: .normal)