1

I've created a view programmatically and now I'm trying to implement this view in one of my Viewcontrollers but unfortunately I can't see it when I run the app.

Here's my code to create the view:

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)

}

}

And then that's the code I used to implement the view in the 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

}

Thank you very much

2
  • I guess you are trying to init the View from a xib, try to use loadNibNamed with your main bundle and then only set it's frame Commented May 22, 2017 at 3:37
  • set translatesAutoresizingMaskIntoConstraints = false for your view Commented May 22, 2017 at 3:46

1 Answer 1

1

When you programatically create a view and using auto layout in your interface builder, you need to set the translatesAutoresizingMaskIntoConstraints to be false (as it defaults to true).

So, just before you do codeStackView.addSubview do:

codeView.translatesAutoresizingMaskIntoConstraints = false

Noticed you had other the other views as well.

In your addingConstraints() method set those as well

codeTextView.translatesAutoresizingMaskIntoConstraints = false
nameLabel.translatesAutoresizingMaskIntoConstraints = false
dateLabel.translatesAutoresizingMaskIntoConstraints = false
mainStackView.translatesAutoresizingMaskIntoConstraints = false
labelStackView.translatesAutoresizingMaskIntoConstraints = false
Sign up to request clarification or add additional context in comments.

4 Comments

No problem (: Be sure to up vote as well if it helped!
Unfortunately I can't because I don't have enough points. I'm quite new to StackOverflow
Can you help me with something else?
Here's a different question which is also about this project I'm currently working on: http://stackoverflow.com/questions/44105862/stackview-doesnt-append-second-view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.