简体   繁体   中英

UIView doesn't appear as subview

I have a cell with subviews.

I can't figure out why the UIView boom isn't visible. 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. Is there a bug in my xcode or something? I have tried restarting xcode, putting the view into a frame, and changing the cell size.

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 .

A way to fix that could be for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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