UIKitMidMCQ

What is `intrinsicContentSize`?

Test your knowledge:

Explanation & Code

Answer: intrinsicContentSize is the natural size a view prefers based on its content — like a label fitting its text, or a button fitting its title. Auto Layout uses this when no explicit size constraint is set.

Code Example:

// UILabel and UIButton have intrinsicContentSize automatically
let label = UILabel()
label.text = "Hello"
print(label.intrinsicContentSize)  // e.g. (33.0, 20.5)

// Custom view — override to declare your preferred size
class BadgeView: UIView {
    var count: Int = 0 {
        didSet { invalidateIntrinsicContentSize() }  // tell Auto Layout size changed
    }

    override var intrinsicContentSize: CGSize {
        let size = max(24, CGFloat(count) * 10)
        return CGSize(width: size, height: 24)
    }
}

// Return this to say "I have no opinion on this dimension"
return CGSize(width: UIView.noIntrinsicMetric, height: 44)

Rate your understanding:

Ready to practice more UIKit?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.