我正在做一个练习来创建任何东西的自定义控件。我的想法是在屏幕中间有一个UIView,下面有一个UILabel。点击视图时,将出现一种随机颜色,标签将更改为十六进制值。尝试创建此自定义控件时,试图以编程方式使UIView居中时遇到问题。我在“colorBox.center”找到一个问题~

import UIKit

@IBDesignable
class Color: UIView {
    private lazy var label : UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.translatesAutoresizingMaskIntoConstraints = false
        label.heightAnchor.constraint(equalToConstant: 25.0).isActive = true
        label.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
        label.font = .systemFont(ofSize: 15.0, weight: UIFontWeightRegular)
        return label
    }()

    private lazy var colorGen : UIView = {
        let colorBox = UIView()
        colorBox.backgroundColor = UIColor.black
        colorBox.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
        colorBox.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
        colorBox.centerXAnchor.constraint(equalTo: colorBox.frame.size.width /2)

    }()

    override init (frame: CGRect) {
        super.init(frame:frame)
        setUpLabel()
        setUpView()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUpLabel()
        setUpView()
    }

我试过使用self.view的答案,但对我不起作用,所以我有点迷路了。

最佳答案

你已经接近了,但是你需要添加标签和视图,这样你就可以约束它们相对于超级视图。。。

@IBDesignable
class ColorView: UIView {

    private lazy var colorLabel : UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.backgroundColor = UIColor.clear
        label.heightAnchor.constraint(equalToConstant: 25.0).isActive = true
        label.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
        label.font = .systemFont(ofSize: 15.0, weight: UIFontWeightRegular)
        return label
    }()

    private lazy var colorGen : UIView = {
        let colorBox = UIView()
        colorBox.translatesAutoresizingMaskIntoConstraints = false
        colorBox.backgroundColor = UIColor.cyan
        colorBox.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
        colorBox.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
        return colorBox
    }()

    override init (frame: CGRect) {
        super.init(frame:frame)
        commonSetup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonSetup()
    }

    func commonSetup() -> Void {
        self.addSubview(colorGen)
        self.addSubview(colorLabel)

        colorGen.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0.0).isActive = true
        colorGen.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0.0).isActive = true
        colorGen.topAnchor.constraint(equalTo: self.topAnchor, constant: 0.0).isActive = true

        colorLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0.0).isActive = true
        colorLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0.0).isActive = true
        colorLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0).isActive = true

        colorLabel.topAnchor.constraint(equalTo: colorGen.bottomAnchor, constant: 0.0).isActive = true

        colorLabel.text = "the label"
    }

}

08-08 02:12