Wednesday, September 10, 2025
HomeiOS Developmentios - If you need a view to drive to width fill...

ios – If you need a view to drive to width fill on its superview, is it really so simple as setting intrinsic to .greatestFiniteMagnitude?


Say you’ve, for instance, a vertical stack view. Its alignment is main (since you need most objects to shrink left).

Nevertheless you’ve one particular sort of merchandise you wish to be full width.

I’ve at all times completed some form of sophisticated answer to attain this:

class FullWidthThing: .. {
    
    ..
        translatesAutoresizingMaskIntoConstraints = false
        
    non-public lazy var fullWidth: NSLayoutConstraint = {
        guard let sv = superview else { return NSLayoutConstraint() }
        let v = widthAnchor.constraint(equalTo: sv.widthAnchor)
        v.isActive = true
        return v
    }()
    
    override func layoutSubviews() {
        if superview != nil {
            _ = fullWidth
        }
        tremendous.layoutSubviews()
    }
}

However is it really simply so simple as setting the intrinsic with to .greatestFiniteMagnitude? Superficial testing reveals this appears to work:

non-public lazy var setup: () = {
    translatesAutoresizingMaskIntoConstraints = false
    setContentCompressionResistancePriority(.required, for: .horizontal)
    ...
    return ()
}()

override var intrinsicContentSize: CGSize {
    var sz = tremendous.intrinsicContentSize
    sz.width = .greatestFiniteMagnitude
    return sz
}

Does anybody decisively know if this works, or is there an issue with doing this, or a standard approach of doing this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments