简体   繁体   English

如何在自定义控件中居中放置UIView?

[英]How can I center UIView in a custom control?

I'm working on a exercise to create a custom control of anything. 我正在做一个练习来创建任何东西的自定义控件。 My idea is to have a UIView in the middle of the screen and a UILabel below it. 我的想法是在屏幕中间有一个UIView,在其下面有一个UILabel。 When you tap on the view a random color will appear with the label changing to its hex value. 当您点击视图时,将出现随机颜色,标签变为其十六进制值。 When trying to create this custom control I'm having a problem trying to center the UIView programmatically. 尝试创建此自定义控件时,尝试以编程方式居中放置UIView时遇到问题。 I get to an issue at `colorBox.center~ 我在`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()
    }

I've tried the answers about using self.view but it doesn't work for me so I'm a bit lost. 我已经尝试过使用self.view的答案,但是它对我不起作用,所以我有点迷路。

You're close, but you need to add the label and the view so you can then constrain them relative to the superview... 您已经接近了,但是您需要添加标签和视图,以便可以相对于超级视图约束它们。

@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"
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM