以编程方式创建的视图不显示

Posted

技术标签:

【中文标题】以编程方式创建的视图不显示【英文标题】:Programmatically created View doesn't show 【发布时间】:2017-05-22 03:20:07 【问题描述】:

我以编程方式创建了一个视图,现在我试图在我的一个 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


非常感谢

【问题讨论】:

我猜你正在尝试从xib 初始化视图,尝试将loadNibNamed 与你的主包一起使用,然后只设置它的框架 设置translatesAutoresizingMaskIntoConstraints = false 供您查看 【参考方案1】:

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

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

codeView.translatesAutoresizingMaskIntoConstraints = false

注意到你也有其他的观点。

在您的 addingConstraints() 方法中也设置这些

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

【讨论】:

没问题(:如果有帮助,请务必投票! 很遗憾我不能,因为我没有足够的积分。我对 *** 很陌生 你能帮我做点别的吗? 这是一个不同的问题,也是关于我目前正在从事的这个项目的:http://***.com/questions/44105862/stackview-doesnt-append-second-view

以上是关于以编程方式创建的视图不显示的主要内容,如果未能解决你的问题,请参考以下文章

UIImageView(以编程方式创建)未显示

不构建视图就不能以编程方式创建 UIViewController 吗?

以编程方式创建的 UIButton 不可见

UIView(放置在滚动视图内)在以编程方式更改高度常量时不显示其子视图

以编程方式创建具有多个标签的 UIView

允许在以模式方式显示的以编程方式创建的视图内自动旋转