无法设置UIView的背景色Swift 5

Can't set UIView's background color Swift 5

我有一个观点,我是这样初始化的:

var view: UIView! = {
    var perm = UIView()

    perm.backgroundColor = UIColor.black

    // enable auto layout
    perm.translatesAutoresizingMaskIntoConstraints = false

    return perm
}()

之后,我将标签作为子视图添加到 view。标签初始化如下:

var title: UILabel! = {
    let perm = UILabel()
    perm.textColor = UIColor.white
    perm.numberOfLines = 0
    perm.font = UIFont.boldSystemFont(ofSize: 20)

    // enable auto layout
    perm.translatesAutoresizingMaskIntoConstraints = false

    return perm
}()

之后,我以编程方式添加了一些约束。当我运行应用程序时,title显示在正确的位置,但view的背景颜色尚未设置。

编辑

这是我设置约束的方式:

view.addSubview(title)

view.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
view.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: image.bottomAnchor).isActive = true

title.leadingAnchor.constraint(equalTo: title.superview!.leadingAnchor, constant: 20).isActive = true
title.trailingAnchor.constraint(equalTo: title.superview!.trailingAnchor, constant: -20).isActive = true
title.bottomAnchor.constraint(equalTo: title.superview!.bottomAnchor, constant: -20).isActive = true

view需要身高

view.heightAnchor.constraint(equalToConstant:200).isActive = true

OR 对于标签高度加上顶部和底部内边距 40

title.topAnchor.constraint(equalTo:view.topAnchor, constant:20).isActive = true