Repository: Automattic/pocket-casts-ios
File: podcasts/LargeListSummaryCellHeaderView.swift (Swift)

Code before:

    func toggleExtras(hidden: Bool) {
        topLabel.isHidden = hidden
        imageView.isHidden = hidden
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    private func setup() {
        addSubview(horizontalStack)
        horizontalStack.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            horizontalStack.leadingAnchor.constraint(equalTo: leadingAnchor),
            horizontalStack.trailingAnchor.constraint(equalTo: trailingAnchor),
            horizontalStack.topAnchor.constraint(equalTo: topAnchor),
            horizontalStack.bottomAnchor.constraint(equalTo: bottomAnchor),
            imageView.widthAnchor.constraint(equalToConstant: 44),
            imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor)
        ])

        verticalStack.addArrangedSubview(topLabel)
        verticalStack.addArrangedSubview(bottomLabel)

        horizontalStack.addArrangedSubview(imageView)
        horizontalStack.addArrangedSubview(verticalStack)
    }
}


Review comment (suggestion):
I think this constraint is generating warnings:
```
Unable to simultaneously satisfy constraints.
	Probably at least one of the constraints in the following list is one you don't want. 
	Try this: 
		(1) look at each constraint and try to figure out which you don't expect; 
		(2) find the code that added the unwanted constraint or constraints and fix it. 
	(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x3035e2670 h=--& v=--& podcasts.ViewControllerContainerContentView:0x115231dc0.height == 100   (active)>",
    "<NSLayoutConstraint:0x3035633e0 podcasts.PodcastImageView:0x1152324c0.width == 44   (active)>",
    "<NSLayoutConstraint:0x303563430 podcasts.PodcastImageView:0x1152324c0.height == podcasts.PodcastImageView:0x1152324c0.width   (active)>",

    "<NSLayoutConstraint:0x303563340 V:|-(0)-[UIStackView:0x115232300]   (active, names: '|':podcasts.LargeListSummaryCellHeaderView:0x115241000 )>",
    "<NSLayoutConstraint:0x303563390 UIStackView:0x115232300.bottom == podcasts.LargeListSummaryCellHeaderView:0x115241000.bottom   (active)>",
    "<NSLayoutConstraint:0x303563700 V:|-(30)-[UIStackView:0x115232a00]   (active, names: '|':podcasts.ThemeableView:0x115232d80 )>",
    "<NSLayoutConstraint:0x303563a20 V:[podcasts.ThemeableCollectionView:0x114952a00]-(40)-|   (active, names: '|':podcasts.ThemeableView:0x115232d80 )>",
    "<NSLayoutConstraint:0x303563a70 V:[UIStackView:0x115232a00]-(16)-[podcasts.ThemeableCollectionView:0x114952a00]   (active)>",
    "<NSLayoutConstraint:0x3035dc500 V:|-(0)-[podcasts.ThemeableView:0x115232d80]   (active, names: '|':podcasts.ViewControllerContainerContentView:0x115231dc0 )>",
    "<NSLayoutConstraint:0x3035a63f0 podcasts.ThemeableView:0x115232d80.bottom == podcasts.ViewControllerContainerContentView:0x115231dc0.bottom   (active)>",
    "<NSLayoutConstraint:0x303567b60 'UISV-canvas-connection' UIStackView:0x115232300.top == _UILayoutSpacer:0x302904780'UISV-alignment-spanner'.top   (active)>",
    "<NSLayoutConstraint:0x30357b2a0 'UISV-canvas-connection' UIStackView:0x115232300.centerY == podcasts.PodcastImageView:0x1152324c0.centerY   (active)>",
    "<NSLayoutConstraint:0x3035e2030 'UISV-canvas-connection' UIStackView:0x115232a00.top == _UILayoutSpacer:0x302904870'UISV-alignment-spanner'.top   (active)>",
    "<NSLayoutConstraint:0x3035e2120 'UISV-canvas-connection' V:[podcasts.LargeListSummaryCellHeaderView:0x115241000]-(>=0)-|   (active, names: '|':UIStackView:0x115232a00 )>",
    "<NSLayoutConstraint:0x3035808c0 'UISV-spanning-boundary' _UILayoutSpacer:0x302904780'UISV-alignment-spanner'.top <= podcasts.PodcastImageView:0x1152324c0.top   (active)>",
    "<NSLayoutConstraint:0x3035e1e00 'UISV-spanning-boundary' _UILayoutSpacer:0x302904870'UISV-alignment-spanner'.top <= podcasts.LargeListSummaryCellHeaderView:0x115241000.top   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x303563430 podcasts.PodcastImageView:0x1152324c0.height == podcasts.PodcastImageView:0x1152324c0.width   (active)>
```

Code after:

    func toggleExtras(hidden: Bool) {
        topLabel.isHidden = hidden
        imageView.isHidden = hidden
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    private func setup() {
        addSubview(horizontalStack)
        horizontalStack.translatesAutoresizingMaskIntoConstraints = false

        // Allow this constraint to break if necessary.
        // Sometimes, the cell height may fluctuate and cause this constraint to throw warnings
        let imageWidthAnchor = imageView.widthAnchor.constraint(equalToConstant: 44)
        imageWidthAnchor.priority = UILayoutPriority(rawValue: 999)

        NSLayoutConstraint.activate([
            horizontalStack.leadingAnchor.constraint(equalTo: leadingAnchor),
            horizontalStack.trailingAnchor.constraint(equalTo: trailingAnchor),
            horizontalStack.topAnchor.constraint(equalTo: topAnchor),
            horizontalStack.bottomAnchor.constraint(equalTo: bottomAnchor),
            imageWidthAnchor,
            imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor)
        ])

        verticalStack.addArrangedSubview(topLabel)
        verticalStack.addArrangedSubview(bottomLabel)

        horizontalStack.addArrangedSubview(imageView)
        horizontalStack.addArrangedSubview(verticalStack)
    }
}