里面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