2017-05-22 5 views
0

Ich habe eine Ansicht programmgesteuert erstellt und jetzt versuche ich, diese Ansicht in einem meiner Viewcontrollers zu implementieren, aber leider kann ich nicht sehen, wenn ich die App ausführen.Programmatisch erstellte Ansicht zeigt nicht

Hier ist mein Code, um die Ansicht zu erstellen:

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) 

} 

} 

Und das ist dann der Code, den ich verwenden, um die Ansicht in dem Viewcontroller zu implementieren:

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 

} 

Vielen Dank

+0

gesetzt Ich glaube, Sie versuchen, die Ansicht von einem 'xib' init, versuchen' loadNibNamed 'mit dem Hauptbündel zu verwenden, und stellen Sie dann nur es Rahmen – Tj3n

+0

Set 'translatedAutoresizingMaskIntoConstraints = false' für Ihre Ansicht –

Antwort

0

Wenn Wenn Sie programmatisch eine Ansicht erstellen und das automatische Layout in Ihrem Interface Builder verwenden, müssen Sie translatesAutoresizingMaskIntoConstraints als false festlegen (da es standardmäßig auf true gesetzt ist).

Also, kurz bevor Sie tun codeStackView.addSubview tun:

codeView.translatesAutoresizingMaskIntoConstraints = false

Bemerken Sie auch andere, die anderen Ansichten haben.

In Ihrem addingConstraints() Methode diese ebenfalls

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

Es funktioniert immer noch nicht, aber danke trotzdem –

+0

Siehe edit @LennartP. – Simon

+0

Kein Problem (: Achten Sie darauf, auch zu wählen, wenn es geholfen hat! – Simon

Verwandte Themen