更改自定义 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: "HasarnAnı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 子类的背景颜色不起作用

我检查了您的代码,您必须像这样设置堆栈,以便自定义视图具有堆栈帧

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

viewDidLoad

override func viewDidLoad() {
let incidentView = WelcomeScreenButtonView(text: "HasarnAnı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

尝试输入 draw 方法

override func draw(_ rect: CGRect) {
}

最新更新