Search code examples
iosswiftuiviewxibios-autolayout

How to add xib subviews with dynamic height to UIView


It should be easy to do and it's very easy in android(with LinearLayout) but I can't figure out how to do it in ios.

I have UIView into which I want to add some dynamic height subviews. Subview is loaded from xib file and contains 2 UILabels with dynamic height.

class Subview: UIView {

    @IBOutlet var contentView: UIView!

    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var desc: UILabel!


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }

    fileprivate func commonInit() {
        Bundle.main.loadNibNamed("Subview", owner: self, options: nil)
        addSubview(contentView)
        self.contentView.frame = self.bounds
        self.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }

}

and subview is added like this

var containerView: UIView

func setupView() {

    let subview1 = Subview()
    subview1.title = "Some title"
    subview1.desc = "Some very long description"

    self.containerView.addSubview(subview1)

}

Issue is that in this case, subview is not displayed. If I add

subview1.frame = CGSize(width: self.bounds.width, height: 100)

subview is displayed but height 100 is not correct

If I try to calculate subview size

let measuredSize = subview1.sizeThatFits(CGSize(width: self.bounds.width, height: CGFloat(MAXFLOAT)))

measured width and size is 0.

Any idea what I am doing wrong here? Thanks!

Edit: This is my subview xib file with constraints enter image description here


Solution

  • You can easily accomplish this by

    1- hooking the constraints properly in the xib file from top to bottom

    2- theView.translatesAutoresizingMaskIntoConstraints = false

    3- set leading , trailing , top constraints ( No height ) , for eample this is a simple xib with one label enter image description here

    When adding it to self.view by

    let vv = (Bundle.main.loadNibNamed("repairView", owner: self, options: nil))?[0] as! repairView;
    
    vv.translatesAutoresizingMaskIntoConstraints = false
    
    vv.aedlb.text = "dsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjh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dsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsvvvvdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsvvvvdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjds"
    
    view.addSubview(vv)
    
    NSLayoutConstraint.activate([
    
        vv.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
        vv.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        vv.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
    
        ])
    

    The result enter image description here