裏面willWillLayoutSubviews()
我打電話UIButton's
方法setTileTheme()
,我創建。結果可以在下面看到 - 重複UILabel
出現在另一個下面。我已經嘗試從viewDidLoad()
等嘗試調用我的方法,但它沒有幫助。UIButton內的重複UILabel
有人知道我爲什麼要面對這個問題嗎?
func setTileTheme(image: UIImage, title: String) {
self.translatesAutoresizingMaskIntoConstraints = false
tintColor = .green
backgroundColor = .white
setBorder(width: 1.5, color: .lightGray)
roundCorners(radius: 5)
self.layer.masksToBounds = true
let width = self.frame.size.width
let height = self.frame.size.height
let offset: CGFloat = width/4.5
let titleLabel = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: width, height: 30))
titleLabel.center = CGPoint(x: width/2, y: height-offset)
titleLabel.text = title
titleLabel.font = titleLabel.font.withSize(15)
titleLabel.textAlignment = .center
titleLabel.textColor = .darkGray
self.insertSubview(titleLabel, at: 0)
imageEdgeInsets = UIEdgeInsets(top: height/8, left: width/4, bottom: height*3/8, right: width/4)
setImage(image, for: .disabled)
setImage(image.withRenderingMode(.alwaysTemplate), for: .normal)
}
多少次被稱爲'setTileTheme()'?多次。根據底部標籤的較深顏色,我會說在同一位置有更多顏色。至少總共稱爲3次。所以使用一個屬性來代替'titleLabel'。 – Larme