What does fixedSize
do?
Does it just fix all layout behaviours to be equal to idealWidth
/idealHeight
?
If so, can we say that
frame(width: w, height:h)
is equivalent to
frame(minWidth: w, idealWidth:w, maxWidth: w, minWidth: h, idealWidth:h, maxWidth: h)
or not?
Normally a View
is constrained by the space proposed to it by its parent, which is called proposedSize
.
Some views use the whole proposed space (Color
, Rectangle
). Others will take their idealSize
, restricted by this proposedSize
(something like min(idealWidth, proposedWidth)
).
The best example is the Text
view.
Let's take a Text
view of 97 points wide :
when the parent view offers it 150 points wide, it uses only 97 points.
When the parent view offers it only 50 points wide, it uses only 50 points wide (and goes over several lines).
If we use the fixedSize
modifier, we make a counter-proposal as Apple says. That is to say that we propose to the view to use its idealWidth
rather than the proposedWidth
.
Applied to a Text view, this means that it will not be constrained by the width offered by the parent view, and therefore will not span several lines.
Applied to a Rectangle
, it becomes more surprising: its idealWidth
seems to be 10 points...
Applied to a UIKit
view (wrapped with UIViewRepresentable
), the idealWidth will be equivalent to intrinsincContentSize.width
.
struct MyViewRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> MyUIView {
let view = MyUIView()
view.backgroundColor = .orange
return view
}
func updateUIView(_ uiView: MyUIView, context: Context) { }
class MyUIView: UIView {
override var intrinsicContentSize: CGSize {
CGSize(width: 70, height: 70)
}
}
}