更改自定义UIView的背景颜色

时间:2018-05-22 08:16:15

标签: ios swift uiview uicolor

我想更改自定义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)

}

P.S:我查了很多关于这个问题的帖子,但大多数都是针对Xib视图和objective-c。因此,我想问一个新问题。

我查过的帖子;

setting self of UIView background color

Setting background color of UIView subclass doesn't work

3 个答案:

答案 0 :(得分:3)

我检查了你的代码,你必须像这样设置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)

}

答案 1 :(得分:2)

实际上问题是你在init()设置背景颜色。

init实际上用于初始化视图/对象(即内存分配给视图/对象)

但是你在init()中设置了颜色,这意味着没有分配视图内存。

这就是在视图中没有设置背景颜色的原因。

您需要在成功初始化视图后进行设置。

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

答案 2 :(得分:0)

尝试输入绘制方法

{{1}}