Emre Önder
Emre Önder

Reputation: 2537

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

Upvotes: 0

Views: 6318

Answers (3)

Mahendra
Mahendra

Reputation: 8924

Actually the problem is that your are setting background color in init().

init actually used to initialise view/object (i.e. memory is allocated to view/object)

But you was setting color in init() it means that view memory is not get allocated.

Thats the reason background color is not get set on view.

you need to set it after view is successfully initialised.

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

Upvotes: 3

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

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)

}

Upvotes: 3

Manish Mahajan
Manish Mahajan

Reputation: 2092

Try by putting in in draw method

override func draw(_ rect: CGRect) {
}

Upvotes: 2

Related Questions