16

I have the following code:

struct ContentView: View {
    var body: some View {
        Text("Test")
            .foregroundColor(.white)
            .font(.subheadline)
            .frame(maxWidth: .infinity)
            .alignmentGuide(.leading) { d in d[.leading] }
            .background(Color(.blue))
    }
}

But as you can see on the image bellow, it does not left align the text. Does anyone knows why this is happening? Maybe because i'm using maxWidth the alignmentGuide thinks it's already left aligned?

result

1 Answer 1

47

Because alignmentGuide has effect in container with other subviews. In this case you need to align Text within own frame.

Here is solution

demo

Text("Test")
    .foregroundColor(.white)
    .font(.subheadline)
    .frame(maxWidth: .infinity, alignment: .leading)   // << here !!
    .background(Color(.blue))
Sign up to request clarification or add additional context in comments.

1 Comment

wow I didnt expect that. I wanted to alphabetically declare them so sequence does matter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.