Fill the blank
Fill the blank so Auto Layout picks up the new size when the text changes.
final class TagView: UIView {
private let label = UILabel()
var text = "" {
didSet {
label.text = text
____
}
}
override var intrinsicContentSize: CGSize {
let size = label.intrinsicContentSize
return CGSize(width: size.width + 16, height: size.height + 8)
}
}Hint 1 of 1
Auto Layout caches the intrinsic size.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A invalidateIntrinsicContentSize()
Correct. It tells Auto Layout to ask for the size again.
Why not the others?
- B
setNeedsDisplay(). That redraws content; it doesn't recompute layout size. - C
layoutIfNeeded(). It runs layout now but with the cached old intrinsic size. - D
sizeToFit(). It changes the frame directly, which Auto Layout overrides.