SwiftUI — Using Frames To Align
Learn to use the Frame Modifier to align you views
Frame is one of the most widely used modifier in SwiftUI. Almost every time that I’ve seen it in code is used to give a view a specific size. Anyway .frame
can be used in a very clever way to align out content.
The Basic
Let’s make an example. Let’s say that we would like to aling a text on the leading edge of the screen. I’ve seen a lot of people use the next approach:
It surely works but it is a bit overkill since you are creating three views.
The elegant and cleaner way that I suggest is the one using the frame modifier:
With the alignment
parameter we can decide the alignment style of our text, or our content. Just add .leading
or .trailing
and…