Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time


If you're coming from a React background, chances are you're used to doing layouts with CSS. SwiftUI is quite different in this regard. I'll be creating various layouts here to demonstrate what's possible in SwiftUI.

  • Using Spacer - Before getting started on anything too complex, it's worth understanding how to use Spacer in conjunction with HStack and VStack to create layouts much like you would with flexbox.

Example layout

Profile layout


Here's a link to a Codesandbox which demonstrates this layout in React.


struct CircleProfile : View {
    var image: Image

    var body: some View {
            .frame(width: 150, height: 150)
            .aspectRatio(CGSize(width: 50, height: 50), contentMode: .fill)
            .overlay(Circle().stroke(Color.white, lineWidth: 4))
            .shadow(radius: 10)


struct ContentView : View {

    func onContact() {
        print("contact me")

    var body: some View {
        VStack {
                .aspectRatio(UIImage(named: "profile-background")!.size, contentMode: .fill)

            CircleProfile(image: Image("ben-again"))
                .offset(x: 0, y: -75)
                .padding(.bottom, -75)

            VStack(alignment: .center) {
                Text("Ben McMahen").font(.title)
                Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
                Button(action: onContact) {
                    Text("Contact Ben")