0

I am having a problem with HStack which has bigger width than the parent view showing centre not leading.

struct WidgetEntryView : View {
    var entry: Provider.Entry
    
    var body: some View {
        ZStack {
            Color(.red)
            HStack {
                Text("haha1")
                    .frame(width: 50)
                Text("haha2")
                    .frame(width: 50)
                Text("haha3")
                    .frame(width: 50)
                Text("haha4")
                    .frame(width: 50)
                Text("haha5")
                    .frame(width: 50)
                Text("haha6")
                    .frame(width: 50)
                Text("haha7")
                    .frame(width: 50)
                Text("haha8")
                    .frame(width: 50)
            }
        }
    }
}

I'm creating a widget with some data on it. It's a medium size widget, and as you can see, HStack width gets bigger than Widget width, so it looks like below.

https://i.stack.imgur.com/uG4wO.png

Currently, anchor point of HStack is the centre, so it's showing from the centre, but I want to move it to leading so that it starts from haha1.

How can I achieve this?

Alejandro
  • 5
  • 3

3 Answers3

0
struct WidgetEntryView : View {
    var entry: Provider.Entry

    var body: some View {
        ZStack {
            Color(.red)
            Color.clear
                .overlay(
                    HStack {
                        Text("haha1")
                            .frame(width: 50)
                        Text("haha2")
                            .frame(width: 50)
                        Text("haha3")
                            .frame(width: 50)
                        Text("haha4")
                            .frame(width: 50)
                        Text("haha5")
                            .frame(width: 50)
                        Text("haha6")
                            .frame(width: 50)
                        Text("haha7")
                            .frame(width: 50)
                        Text("haha8")
                            .frame(width: 50)
                    }, alignment: .leading
                )

        }
    }
}

did a trick.

Alejandro
  • 5
  • 3
0

Both the HStack and ZStack expect to be as wide as their contents, and so both end up being larger than the widget width.

The way to prevent that is to make sure its content has a flexible size / smaller min width. The simplest would be adding a frame on the stack that has a min width and aligns the stack within it along its leading edge:

ZStack {
    Color(.red)
    HStack {
        ...
    }
    .frame(minWidth: 0, alignment: .leading)
}
Taylor
  • 3,183
  • 17
  • 18
-1

Another way: Use GeometryReader

When using a GeometryReader, child objects are automatically aligned to the left. (This may also be a trick)

struct WidgetEntryView : View {
    var entry: Provider.Entry
    
    var body: some View {
        GeometryReader { _ in  // Wrap with GeometryReader.
            ZStack {
                Color(.red)
                
                HStack {
                    Text("haha1")
                        .frame(width: 50)
                    Text("haha2")
                        .frame(width: 50)
                    Text("haha3")
                        .frame(width: 50)
                    Text("haha4")
                        .frame(width: 50)
                    Text("haha5")
                        .frame(width: 50)
                    Text("haha6")
                        .frame(width: 50)
                    Text("haha7")
                        .frame(width: 50)
                    Text("haha8")
                        .frame(width: 50)
                }
            }
        }
    }
}
Cheolhyun
  • 169
  • 1
  • 7