Why overlay is not on top

Please see image.

I want the text "one case per million to extend". but in this scenario, it seems that the image overlay is occupying space inside the the view where the text are located

Same as "One Death Per People". I think the first line should be "One Death Per" and maybe because the image is big that it somehow is occupying its area? (But i placed the image in overlay so it should be rendered no top)

I think the word "Per" should still in in the first line.

Any idea what could be the problem?

This is my code

struct Stat: View {
   
  var body: some View {
    VStack {
      Text("One Case Per People")
        .frame(maxWidth: .infinity, alignment: .leading)
        .font(.system(size: 19))
      Text("2")
        .frame(maxWidth: .infinity, alignment: .leading)
        .font(.system(size: 15))
      Spacer()
    }
    .padding(8)
    .frame(minHeight: 100)
    .cornerRadius(8)
    .background(Color.gray)
    .overlay(
      Image(systemName: "person.3.fill")
        .resizable()
        .frame(width: 60, height: 60, alignment: .trailing)
        .padding(8),
      alignment: .bottomTrailing
    )
  }
}

I used a grid to display them

ScrollView {
        LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())]) {
            Stat()
            Stat()
            Stat()
            Stat()
          }
        }
      }

Thoughts?

Thoughts?

You should show the code that draws the other cells. With what you show, that should draw for identical cells.

Changed to focus on only On Case Per People text sample.

Accepted Answer

I think your problem is in the .padding(8) line. When you have that padding it adds 8px to all four external sides of the Text label, making it too small to fit the text on one line. You can achieve your desired look by reducing the font size, too, but that's probably not of much use.

I've just put this into a new project, and Xcode is showing the preview in an iPhone 14 Pro (or Max) where the result is perfectly fine, because that device is quite wide. I've added .frame(width 380) to the ScrollView so it can be seen on a smaller screen. I've given the various elements a background colour so you can see where they interact:

struct ContentView: View {
    var body: some View {
		ScrollView {
			LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())]) {
				Stat()
				Stat()
				Stat()
				Stat()
			}
		}
		.frame(width: 380)  // Change this to simulate smaller devices
	}
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct Stat: View {
	var body: some View {
		VStack {
			Text("One Case Per People")
				.frame(maxWidth: .infinity, alignment: .leading)
				.font(.system(size: 19))
				.background(Color.red)
			Text("2")
				.frame(maxWidth: .infinity, alignment: .leading)
				.font(.system(size: 15))
				.background(Color.green)
			Spacer()
		}
//		.padding(8)  // If you use this padding instead of the below two lines you can see your problem
		.padding(.top, 8)
		.padding([.leading, .trailing], 3)
		.frame(minHeight: 100)
		.cornerRadius(8)
		.background(Color.blue)
		.overlay(
			Image(systemName: "person.3.fill")
				.resizable()
				.frame(width: 60, height: 40, alignment: .trailing). // I reduced the height to 40 here, as 60 seems too tall for this symbol
				.padding(8)
				.background(Color.yellow)
			,
			alignment: .bottomTrailing
		)
	}
}

I used your suggestion. I think this is more of an internal thing. I tried it with another kind of text the one case per people still has the same result. Cannot figure out why the per shouldnt be in the first line.

but the red background clearly shows the text width extends to the end so the Per should be there. But since it's not, this is more of an internal logic inside how Text wraps the string I guess?

Why overlay is not on top
 
 
Q