Search code examples
iosswiftuiviewuicolor

Change background color of custom UIView


I want to change background color of my custom UIView. I'm writing background color code inside init function but nothing happens. I think It is because UIView's frame hasn't been set when I declare background color. However, I don't know how can I solve it.

My Custom Class Code

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

}
}

I also tried to do it where I declare this custom view

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

But nothing happened. Btw, I'm putting this View inside UIStackView.

EDIT: As your answers, I think I have problem where I declare it.

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)

}

P.S: I checked lots of posts about this problem but most of them are for Xib views and in objective-c. Therefore, I wanted to ask as a new question.

Posts I checked;

setting self of UIView background color

Setting background color of UIView subclass doesn't work


Solution

  • I checked your code your have to set Stack like that so that custom-view have 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)
    
    }