简体   繁体   English

UIView不会显示为子视图

[英]UIView doesn't appear as subview

I have a cell with subviews. 我有一个带子视图的单元格。

I can't figure out why the UIView boom isn't visible. 我不知道为什么看不到UIView boom Here is my code: 这是我的代码:

let separator: UIView = {
    let view = UIView()
    view.backgroundColor = .yellow
    return view
}()

let boom: UIView = {
    let b = UIView()
    b.backgroundColor = .red
    return b
}()

override func setupViews() {
    super.setupViews()

    addSubview(separator)
    addSubview(setNumberView)
    addSubview(boom)

    backgroundColor = .orange

    addConstraintsWithFormat("H:|-20-[v0]", views: boom)
    addConstraintsWithFormat("V:|-20-[v0]", views: boom)

    addConstraintsWithFormat("H:|[v0]|", views: separator)
    addConstraintsWithFormat("V:[v0(10)]|", views: separator)

separator shows up as it is supposed to. separator按预期显示。 Is there a bug in my xcode or something? 我的xcode中有错误吗? I have tried restarting xcode, putting the view into a frame, and changing the cell size. 我尝试重新启动xcode,将视图放入框架中,并更改单元格大小。

You are not setting any width or height to your view. 您没有为视图设置任何宽度或高度。

To properly setup the position of a view, you have to specify the horizontal position, the vertical position, the width and height. 要正确设置视图的位置,必须指定水平位置,垂直位置,宽度和高度。

The separator correctly specifies all of them, the view is missing constraints for width and height . separator正确指定了所有separator ,该view缺少widthheight约束。

A way to fix that could be for example: 例如,一种解决方法:

addConstraintsWithFormat("H:|-20-[v0]-20-|", views: boom)
addConstraintsWithFormat("V:|-20-[v0(100)]", views: boom)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM