I'm using SwiftUI to create a Widget and I'm struggling with something that is pretty simple and straightforward in Swift.
I have 2 images that are next to each other and I want them with the exact same size with aspect ratio fill but without getting out of bounds.
How it works at the moment is I have a view that is an Image and a Text. And then a parent view that has a HStack with 2 of those views.
Basically what I want to achieve is this view but with the images correctly:
This is done doing this:
VStack() {
Image(uiImage: image)
.resizable()
Text(affirmation.title)
.font(.body)
.foregroundColor(Color(UIColor.MAPurple()))
}
}
And for the parent view:
HStack {
Spacer()
CardView(text: text, image: firstImage)
Spacer()
CardView(text: text, image: secondImage)
Spacer()
}
If I add the aspect ratio to fill as I would do in Swift, this is how it looks:
Update
Adding a minimal reproducible example:
struct CardView: View {
let text: String
let image: UIImage
var body: some View {
VStack(alignment: .leading) {
Image(uiImage: image)
.resizable()
// .aspectRatio(contentMode: .fill)
.clipped()
Text(text)
.font(.body)
.multilineTextAlignment(.leading)
.foregroundColor(Color.blue)
}
}
}
struct ParentView: View {
let firstText: String = "This is something"
let firstImage: UIImage
let secondText: String = "This is something else"
let secondImage: UIImage
let top: String = "This is the top string"
var body: some View {
VStack {
Spacer()
Spacer()
Text(top)
.font(.largeTitle)
.foregroundColor(Color(UIColor.MAPurple()))
.padding(.all, 10)
.minimumScaleFactor(0.4)
Spacer()
Spacer()
HStack {
Spacer()
CardView(text: firstText, image: firstImage)
Spacer()
CardView(text: secondText, image: secondImage)
Spacer()
}
Spacer()
Spacer()
}
.background(Color.yellow)
.edgesIgnoringSafeArea(.all)
}
}