1

When use ScrollView and ForEach looks like the following code :

ScrollView(.horizontal,showsIndicators: false) { HStack { ForEach(self.QUOTES, id: \.self) { qt in Text(qt) } } } 

Its ok and no problem. But when I want to use any code (like adding a image) in ForEach block like the following code :

ScrollView(.horizontal,showsIndicators: false) { HStack { ForEach(self.QUOTES, id: \.self) { qt in Text(qt) Image(systemName: "a.square.fill") .resizable() .frame(width: 40, height: 40) } } } 

I get the following error while writing : Unable to infer complex closure return type; add explicit type to disambiguate

1
  • 1
    Have you tried putting your Text and Image in some kind of stack? Stack or Hstack? Honestly, and meant with no offense, this is pretty basic SwiftUI. Maybe your HStack is too... outer... in the FroEach. But stat with something more simple≥ Maybe A List. Or maybe a View. Move that into it's own struct. (My choice? a Button. Who cares if it doesn't do anything. Get it laid out like you wish.) As is, you need to either Group your code or use a stack of some source. But also? As is, your question doesn't help to say which. Commented Sep 13, 2019 at 17:34

1 Answer 1

4

The ForEach struct needs to contain a single view so you'll need to group the text and image or embed them in a stack depending on how you want the layout to render.

ScrollView(.horizontal,showsIndicators: false) { HStack { ForEach(self.QUOTES, id: \.self) { qt in Group { Text(qt) Image(systemName: "a.square.fill") .resizable() .frame(width: 40, height: 40) } } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.