簡體   English   中英

自定義 UIButton 類不顯示標題

[英]Custom UIButton class does not show title

我創建了一個自定義UIButton類。 但是標題不顯示。 我只得到背景顏色和圓角。 但是標題不可見。

同樣,當我在模擬器 iOS 13+ 中運行它時,它的標題顯示為白色。 但是當我在我的設備(即 iOS 12.4)中運行時,它不起作用。

這是我的代碼:

public class CornerButton : UIButton {

    public override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
        self.layoutIfNeeded()

    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
        self.layoutIfNeeded()

    }

    func setup() {
        self.titleLabel?.font = UIFont(name: "Poppins-SemiBold", size: 12)
        self.backgroundColor = UIColor(named: "BarColor")
        self.setTitleColor(.white, for: .normal) // this line is not working
        self.clipsToBounds = true

    }

    public override func layoutSubviews() {
        self.roundCorners(corners: [.topRight,.bottomLeft], radius: 10)
    }

}

你需要調用super.layoutSubviews()

public override func layoutSubviews() {
    super.layoutSubviews()
    self.roundCorners(corners: [.topRight,.bottomLeft], radius: 10)
}

順便說一句,當您需要制作取決於視圖的實際測量邊界的東西時,您不需要layoutSubviews ,並且當您設置靜態半徑時,您可以省略它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM