SwiftUI — Using Frames To Align

Learn to use the Frame Modifier to align you views

Alessandro Manilii
3 min readJan 27, 2023
Photo by Emma Pollard: https://www.pexels.com/it-it/foto/foto-di-pianta-a-foglia-di-formaggio-svizzero-verde-rettangolare-montata-sul-muro-1534924/

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…

--

--

Alessandro Manilii

I’m an Italian professional iOS Developer, iOS Tech Lead at Wakala — Join Medium from the following link: https://medium.com/@alessandromanilii/membership