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)
}
を役に立てば幸い?上書きすることができますか複数回。下のラベルの濃い色によれば、同じ位置にもっと多くの色があると言えます。少なくとも合計で3回呼び出されます。したがって、 'titleLabel'の代わりにプロパティを使用してください。 – Larme