Align content to left - SwiftUI iOS 14 Widget

1.7k views Asked by At

I'm building a widget for iOS 14 and would like some of the content to align to the left, with some of the other content on the right. How would I do this?

Here's my code now:

ZStack {
            HStack(alignment: .center, spacing: nil, content: {
                
                VStack(alignment: .leading, spacing: nil, content: {
                    Text("Heading")
                        .zIndex(2)
                        .shadow(radius: 5)

                    
                    Text("Subtitle")
                        .zIndex(1)
                        .shadow(radius: 2.5)

                })
                
            })
            Image("Image")
                .zIndex(-1)
        }

This creates this:enter image description here

I would like the Heading and Subtitle text aligned to the very left of the widget, so I can add more information on the center / right side.

Thanks in advance! I'm fairly new to SwiftUI, so it's all a bit of learning.

1

There are 1 answers

0
Asperi On

Add spacer in HStack

        HStack(alignment: .center, spacing: nil, content: {
            
            VStack(alignment: .leading, spacing: nil, content: {
                Text("Heading")
                    .zIndex(2)
                    .shadow(radius: 5)

                
                Text("Subtitle")
                    .zIndex(1)
                    .shadow(radius: 2.5)

            })
            Spacer()     // << here !!                
        })