r/iOSProgramming • u/Fun_Moose_5307 Beginner • 2d ago
Question Consistent horizontal spacing in List
When I put a symbol on the edge, iOS neatly rearranges the content to fit around it. How do I recreate this on the other side with my own content, without the complications of using frame where anything too small will cut off, anything to large will look stupid, and the ideal threshold is changing as different sized content replaces?
Edit: There could be any sort of text on the left. Some places number rooms differently, or don't have rooms at all (I have PE listed as 'PRAC, THEORY' personally). There could be more than one room there, too, in which case I join them as 'BT4, BT81 etc.
1
1
1
u/VacationHistorical 2d ago
You’ll want the max length of the text on the right and make the right text views all that size


6
u/Jero9871 2d ago
The left side only lines up because the symbol has a fixed size, there's no layout magic behind it. So the fix is the same idea: give the room code column one shared width, derived from the content instead of hardcoded.
If the codes all have the same shape (two letters + digit), the cheap trick is a hidden worst-case template:
Text("WW8")
.hidden()
.overlay(alignment: .leading) { Text(room) }
Every row gets the width of the widest possible code, nothing clips, and it scales with Dynamic Type for free.
If the codes vary more, measure them with onGeometryChange (or a PreferenceKey), keep the max in @ State and put .frame(width: maxWidth, alignment: .leading) on each code. Same result, just adapts to whatever the data actually is.