How To: Self-Sizing Custom-View from XIB with StackView View in iOS
<p>I've recently started diving into iOS development with Swift again and am now looking into a custom UIControl. For layout and flexibility this custom view is built using a StackView.</p>

<h2>What I want to achieve ...</h2>

<p>... is basically to simply have the same functionality with the StackView that I usually have when I drag it directly into the storyboard - where it resizes to fit no problem. This essentially the same as self-sizing tableview cells.</p>

<h2>Showcase</h2>

<p>I can reduce my CustomView to a simple example: It's a StackView with three labels in it, <code>Alignment</code> set to <code>Center</code>, <code>Distribution</code> <code>Equal Centering</code>. The StackView would be pinned to left, right and top layout guides (or trailing, leading and top). This I can easily re-create with a CustomView, loaded from XIB translate into a CustomView with the same parameters - I attached both as screenshot.</p>

<p><a href="https://i.stack.imgur.com/FjmwP.png" rel="noreferrer">Screenshot of Simple StackView</a></p>

<p><a href="https://i.stack.imgur.com/5s65H.png" rel="noreferrer">Screenshot of CustomView</a></p>

<h2>Loading the XIB File and setting it up.</h2>

<pre><code>import UIKit

@IBDesignable
class CustomView: UIView {

    // loaded from NIB
    private weak var view: UIView!

    convenience init() {
        self.init(frame: CGRect())
    }

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

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

    override func layoutSubviews() {
        super.layoutSubviews()

        // we need to adjust the frame of the subview to no longer match the size used
        // in the XIB file BUT the actual frame we got assinged from the superview
        self.view.frame = bounds
    }

    private func loadNib ()  {
        self.view = Bundle (for: type(of: self)).loadNibNamed(
            "CustomView", owner: self, options: nil)! [0] as! UIView
        self.addSubview(self.view)
    }
}
</code></pre>

<p>What you can see here is that I simply load the XIB file, override layout Subviews to adapt the frame to the actual view and that's it.</p>

<h2>Questions</h2>

<p>So my questions are related to how this is usually approached. The labels in my StackView have an intrinsic content size, i.e., usually the StackView simply adapts to the height of the labels without complaining - IF you design it in the storyboard. It doesn't though, if you put all of it in a XIB and leave the layout as it is - the IB will complain about an unknown height if the view is pinned three ways as in the screenshot (top, leading, trailing).</p>

<p>I read the Auto-Layout Guide and watched the WWDC videos "Mysteries of AutoLayout" but the topic is still quite new to me and I don't think I got the right approach yet. So this is where I need help</p>

<h3>(1) Should I set constraints and disable <code>translatesAutoresizingMaskIntoConstraints</code> ?</h3>

<p>What I first could and should do is to set  <code>translatesAutoresizingMaskIntoConstraints</code> <code>false</code> such that I don't get auto-generated constraints and define my own constraints. So I could change <code>loadNib</code> into this:</p>

<pre><code>private func loadNib ()  {
    self.view = Bundle (for: type(of: self)).loadNibNamed(
        "CustomView", owner: self, options: nil)! [0] as! UIView
    self.view.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(self.view)

    self.addConstraint(NSLayoutConstraint (
        item: self
        , attribute: .bottom
        , relatedBy: .equal
        , toItem: self.view
        , attribute: .bottom
        , multiplier: 1.0
        , constant: 0))

    self.addConstraint(NSLayoutConstraint (
        item: self
        , attribute: .top
        , relatedBy: .equal
        , toItem: self.view
        , attribute: .top
        , multiplier: 1.0
        , constant: 0))

    self.addConstraint(NSLayoutConstraint (
        item: self.view
        , attribute: .leading
        , relatedBy: .equal
        , toItem: self
        , attribute: .leading
        , multiplier: 1.0
        , constant: 0))

    self.addConstraint(NSLayoutConstraint (
        item: self.view
        , attribute: .trailing
        , relatedBy: .equal
        , toItem: self
        , attribute: .trailing
        , multiplier: 1.0
        , constant: 0))
}
</code></pre>

<p>Basically I did stretch the view to fit the full width of my UIView in the Storyboard and restrained it's top and bottom to top and bottom of the StackView.</p>

<p>I had tons of problems with XCode 8 crashing on me when using this one so I'm not quite sure what to make of it.</p>

<h3>(2) Or should I override <code>intrinsicContentSize</code> ?</h3>

<p>I could also simply override <code>intrinsicContentSize</code> by checking the height of all my <code>arrangedSubviews</code> and take the one that is the highest, set my view height to it:</p>

<pre><code>override var intrinsicContentSize: CGSize {
    var size = CGSize.zero

    for (_ , aSubView) in (self.view as! UIStackView).arrangedSubviews.enumerated() {
        let subViewHeight = aSubView.intrinsicContentSize.height
        if  subViewHeight &gt; size.height {
            size.height = aSubView.intrinsicContentSize.height
        }
    }

    // add some padding
    size.height += 0
    size.width = UIViewNoIntrinsicMetric
    return size
}
</code></pre>

<p>While this surely gives me more flexibility I'd also have to invalidate the intrinsic content size, check the Dynamic Type and lots of other stuff I'd rather avoid.</p>

<h3>(3) Am I even loading the XIB in the "proposed" way ?</h3>

<p>Or is there a better way, e.g. by using <code>UINib.instantiate</code>? I really couldn't find a best-practice way to do that.</p>

<h3>(4) Why do I have to do this in the first place?</h3>

<p>Really, why? I only moved the StackView into a CustomView. So why does the StackView stop adapting itself now, why do I have to set the top and bottom constraints?</p>

<p>What was helping the most so far is that key-phrase in <a href="https://stackoverflow.com/a/24441368">https://stackoverflow.com/a/24441368</a> : </p>

<blockquote>
  <p>"In general, auto layout is performed in a top-down fashion. In other
  words, a parent view layout is performed first, and then any child
  view layouts are performed."</p>
</blockquote>

<p>.. but I'm really not so sure about it. </p>

<h3>(5) And why do I have to add the constraints in code?</h3>

<p>Assuming I disable <code>translatesAutoresizingMaskIntoConstraints</code> then I can't set constraints like for tableview cells in IB? It'll always translate into something like label.top = top and thus the label would be stretched instead of the StackView inheriting the size/height. Also I can't really pin the StackView to it's container view in the IB for the XIB file.</p>

<p>Hope someone can help me out there. Cheers!</p>