Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align content to left - SwiftUI iOS 14 Widget

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.

like image 289
Noah Evans Avatar asked Sep 28 '20 16:09

Noah Evans


1 Answers

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 !!                
        })
like image 58
Asperi Avatar answered Sep 23 '22 00:09

Asperi