以编程方式创建的View不显示

时间:2017-05-22 03:20:08

标签: ios view swift3 xcode8 visible

我已经以编程方式创建了一个视图,现在我正在尝试在我的一个Viewcontrollers中实现此视图,但遗憾的是,当我运行该应用程序时,我无法看到它。

这是我创建视图的代码:

class CodeView: UIView {

let codeTextView = UITextView()
let nameLabel = UILabel()
let dateLabel = UILabel()
let mainStackView = UIStackView()
let labelStackView = UIStackView()

let size = CGRect(x: 0, y: 0, width: 250, height: 175)

public init(name: String?, date: String?, code: String) {

    if let name = name {
        nameLabel.text = name
    } else {
        nameLabel.isHidden = true
    }

    if let date = date {
        dateLabel.text = date
    } else {
        dateLabel.isHidden = true
    }

    codeTextView.text = code

    super.init(frame: size)

    subview()
    setup()
    addingConstraints()

}

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

func setup() {

    codeTextView.textColor = .white
    codeTextView.backgroundColor = UIColor(red: 2/255, green: 11/255, blue: 57/255, alpha: 0.75)

    dateLabel.font = UIFont(name: "Avenir-Light", size: 17)



}

func addingConstraints() {

    var constraints = [NSLayoutConstraint]()

    let nameLabelConstraintWidth = NSLayoutConstraint(item: nameLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 250)
    let nameLabelConstraintHeight = NSLayoutConstraint(item: nameLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 20)

    let dateLabelConstraintWidth = NSLayoutConstraint(item: dateLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 250)
    let dateLabelConstraintHeight = NSLayoutConstraint(item: dateLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 20)

    let labelStackViewConstraintLeft = labelStackView.leadingAnchor.constraint(equalTo: (labelStackView.superview?.leadingAnchor)!)
    let labelStackViewConstraintRight = labelStackView.trailingAnchor.constraint(equalTo: (labelStackView.superview?.trailingAnchor)!)
    let labelStackViewConstraintBottom = labelStackView.bottomAnchor.constraint(equalTo: (labelStackView.superview?.bottomAnchor)!)

    let codeTextViewConstraintLeft = codeTextView.leadingAnchor.constraint(equalTo: (codeTextView.superview?.leadingAnchor)!)
    let codeTextViewConstraintRight = codeTextView.trailingAnchor.constraint(equalTo: (codeTextView.superview?.trailingAnchor)!)
    let codeTextViewConstraintTop = codeTextView.topAnchor.constraint(equalTo: (codeTextView.superview?.topAnchor)!)

    let codeTextViewConstraintWidth = NSLayoutConstraint(item: codeTextView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 250)
    let codeTextViewConstraintHeight = NSLayoutConstraint(item: codeTextView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 125)

    let mainStackViewConstraintTop = mainStackView.topAnchor.constraint(equalTo: self.topAnchor)
    let mainStackViewConstraintBottom = mainStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
    let mainStackViewConstraintLeft = mainStackView.leadingAnchor.constraint(equalTo: self.leadingAnchor)
    let mainStackViewConstraintRight = mainStackView.trailingAnchor.constraint(equalTo: self.trailingAnchor)

    constraints.append(contentsOf: [nameLabelConstraintWidth, nameLabelConstraintHeight, dateLabelConstraintWidth, dateLabelConstraintHeight, labelStackViewConstraintLeft, labelStackViewConstraintRight, labelStackViewConstraintBottom, codeTextViewConstraintLeft, codeTextViewConstraintRight, codeTextViewConstraintTop, codeTextViewConstraintWidth, codeTextViewConstraintHeight, mainStackViewConstraintTop, mainStackViewConstraintBottom, mainStackViewConstraintLeft, mainStackViewConstraintRight])

    NSLayoutConstraint.activate(constraints)

}

func subview() {
    self.addSubview(nameLabel)
    self.addSubview(dateLabel)
    self.addSubview(codeTextView)
    self.addSubview(mainStackView)
    self.addSubview(labelStackView)

    labelStackView.addArrangedSubview(nameLabel)
    labelStackView.addArrangedSubview(dateLabel)

    mainStackView.addArrangedSubview(codeTextView)
    mainStackView.addArrangedSubview(labelStackView)

}

}

然后那是我用来在Viewcontroller中实现视图的代码:

let codeView = CodeView(name: "Name", date: "Today", code: "Just some code")
@IBOutlet weak var codeStackView: UIStackView!

override func viewDidLoad() {
    super.viewDidLoad()

    codeView.layer.cornerRadius = 15
    codeView.clipsToBounds = true

    codeView.codeTextView.layer.cornerRadius = 15

    codeStackView.addSubview(codeView)
    codeStackView.addArrangedSubview(codeView)
    codeStackView.alignment = .center

}

非常感谢

1 个答案:

答案 0 :(得分:0)

如果以编程方式在界面构建器中创建视图并使用自动布局,则需要将translatesAutoresizingMaskIntoConstraints设置为false(默认为true)。

所以,就在你做codeStackView.addSubview之前:

codeView.translatesAutoresizingMaskIntoConstraints = false

注意到你还有其他观点。

addingConstraints()方法中设置

codeTextView.translatesAutoresizingMaskIntoConstraints = false
nameLabel.translatesAutoresizingMaskIntoConstraints = false
dateLabel.translatesAutoresizingMaskIntoConstraints = false
mainStackView.translatesAutoresizingMaskIntoConstraints = false
labelStackView.translatesAutoresizingMaskIntoConstraints = false