簡體   English   中英

以編程方式創建的視圖不顯示

[英]Programmatically created View doesn't show

我已經以編程方式創建了一個視圖,現在我試圖在一個Viewcontroller中實現該視圖,但是不幸的是,當我運行該應用程序時,我看不到它。

這是我創建視圖的代碼:

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

}

非常感謝你

當您以編程方式創建視圖並在界面生成器中使用自動布局時,需要將translatesAutoresizingMaskIntoConstraints設置為false(默認為true)。

因此,在執行codeStackView.addSubview之前,請執行codeStackView.addSubview操作:

codeView.translatesAutoresizingMaskIntoConstraints = false

注意您還有其他觀點。

在您的addingConstraints()方法中addingConstraints()設置它們

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM