代码之家  ›  专栏  ›  技术社区  ›  petaire

为什么我看不到这个标签上的约束?

  •  0
  • petaire  · 技术社区  · 9 年前

    class A19: UIView {
    let titleLabel : UILabel = {
        let label = UILabel()
        label.text = "test"
        label.textColor = .white
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: 414, height: 250))
        backgroundColor = .black
        addSubview(titleLabel)
        titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 20).isActive = true
        titleLabel.sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    }
    

    这有什么不对?

    编辑:所以我把操场,让每个人都看到我没有疯! enter image description here

    1 回复  |  直到 9 年前
        1
  •  1
  •   vacawama    9 年前

    view.layoutIfNeeded()
    

    创建要踢的视图后 付诸行动。