簡體   English   中英

更改自定義UIView的背景顏色

[英]Change background color of custom UIView

我想更改自定義UIView背景顏色。 我正在init函數中編寫背景色代碼,但沒有任何反應。 我認為這是因為在聲明背景色時尚未設置UIView's框架。 但是,我不知道該如何解決。

我的自定義班級代碼

class WelcomeScreenButtonView: UIView {

private lazy var label = UILabel()
private lazy var logoImage = UIImageView()

override init(frame: CGRect) {
    super.init(frame: frame)
    translatesAutoresizingMaskIntoConstraints = false
    setupUI()
    setupConstraints()
}
public convenience init(text: String, imageName: String){
    self.init()
    self.label.text = text
    self.logoImage.image = UIImage(named: imageName)?.withRenderingMode(.alwaysTemplate)
}
required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupConstraints(){
    logoImage.anchor(self.topAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    label.anchor(self.logoImage.bottomAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 10, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
}
func setupUI(){
    layer.cornerRadius = 6.0
    logoImage.contentMode = .scaleAspectFit
    logoImage.translatesAutoresizingMaskIntoConstraints = false
    logoImage.tintColor = UIColor.white
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textAlignment = .center
    label.numberOfLines = 2
    label.lineBreakMode = .byWordWrapping
    label.textColor = UIColor.white
    label.font = GeneralFont.lightFont.withSize(13)

    addSubview(label)
    addSubview(logoImage)
    backgroundColor = UIColor.blue

}
}

我還嘗試在聲明此自定義視圖的地方進行操作

let myCustomView = WelcomeScreenButtonView()
myCustomView.backgroundColor = UIColor.blue

但是什么也沒發生。 順便說一句,我把這個視圖放在UIStackView

編輯:作為您的答案,我認為我在聲明它的地方有問題。

override func viewDidLoad() {
    let incidentView = WelcomeScreenButtonView(text: "Hasar\nAnında", imageName: "hasar")
    stackView.axis  = UILayoutConstraintAxis.horizontal
    stackView.distribution = UIStackViewDistribution.fillProportionally
    stackView.alignment = UIStackViewAlignment.bottom
    stackView.spacing = 0.0
    stackView.addArrangedSubview(incidentView)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.anchor(nil, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 90, rightConstant: 0, widthConstant: 0, heightConstant: 90)

}

PS:我檢查了很多有關此問題的帖子,但其中大多數是針對Xib的觀點和Objective-C的。 因此,我想提出一個新問題。

我檢查過的帖子;

設置UIView背景顏色的自身

設置UIView子類的背景顏色不起作用

我檢查了您的代碼,您必須像這樣設置Stack,以便自定義視圖具有stack-frame

       stackView.distribution = UIStackViewDistribution.fill
        stackView.alignment = UIStackViewAlignment.fill

viewDidLoad

override func viewDidLoad() {
    let incidentView = WelcomeScreenButtonView(text: "Hasar\nAnında", imageName: "hasar")
         stackView.axis  = UILayoutConstraintAxis.horizontal
            stackView.distribution = UIStackViewDistribution.fill
            stackView.alignment = UIStackViewAlignment.fill
            stackView.spacing = 0.0
    stackView.addArrangedSubview(incidentView)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.anchor(nil, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 90, rightConstant: 0, widthConstant: 0, heightConstant: 90)

}

實際上,問題是您正在init()中設置背景色。

實際上用於初始化視圖/對象的init (即,將內存分配給視圖/對象)

但是您在init()中設置了顏色,這意味着未分配視圖內存。

多數民眾贊成在沒有設置背景色的原因。

視圖初始化成功后,需要進行設置。

let view = MyCustomView()
view.backgroundColor = UIColor.red

嘗試使用繪制方法

override func draw(_ rect: CGRect) {
}

暫無
暫無

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

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