如何在外部 HStack 中右对齐图像? — Swift
How can I right-align images within an outer HStack? — Swift
对齐我的图像,但我找不到方法。任何见解将不胜感激。谢谢。我试过(对齐方式:.trailing),但这也不起作用。 Here's a link to my view
struct ArticleRowView: View {
let article: Article
var body: some View {
VStack(alignment: .leading){
HStack(spacing: 16){
VStack(alignment: .leading, spacing: 4){
Text(article.siteText)
.font(Font.custom("Inter-Regular.ttf", size: 12))
.padding([.bottom], 4)
Text(article.titleText)
.font(Font.custom("Inter-SemiBold.ttf", size: 20))
.minimumScaleFactor(0.75)
.padding([.bottom], 4)
HStack(spacing: 8){
Text(article.dateText)
.font(Font.custom("Inter-Regular.ttf", size: 12))
Image("rect")
Text(article.tickerText)
.font(Font.custom("Inter-Regular.ttf", size: 12))
Image("green")
.padding([.leading, .trailing], -8)
Text("2.33%")
.font(Font.custom("Inter-Regular.ttf", size: 12))
.foregroundColor(Color(red: 0.345, green: 0.50, blue: 0.155, opacity: 1.0))
}.lineLimit(1)
.minimumScaleFactor(0.75)
}
AsyncImage(url: article.imageURL) {image in
image.resizable()
.frame(width: 64, height: 64, alignment: .trailing)
.aspectRatio(contentMode: .fill)
.cornerRadius(6)
} placeholder: {
ProgressView()
}
}.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
.listRowSeparator(.hidden)
Image("rect-hor")
}
}
}
在 VStack
和 AsyncImage
之间使用 Spacer()
,这将填满 HStack
中可用的水平 space,并将您的图像推送到后缘:
VStack(alignment: .leading, spacing: 4) {
//content
}
Spacer() //<-- Here
AsyncImage(url: article.imageURL) { image in
image.resizable()
.frame(width: 64, height: 64)
.aspectRatio(contentMode: .fill)
.cornerRadius(6)
} placeholder: {
ProgressView()
}
对齐我的图像,但我找不到方法。任何见解将不胜感激。谢谢。我试过(对齐方式:.trailing),但这也不起作用。 Here's a link to my view
struct ArticleRowView: View {
let article: Article
var body: some View {
VStack(alignment: .leading){
HStack(spacing: 16){
VStack(alignment: .leading, spacing: 4){
Text(article.siteText)
.font(Font.custom("Inter-Regular.ttf", size: 12))
.padding([.bottom], 4)
Text(article.titleText)
.font(Font.custom("Inter-SemiBold.ttf", size: 20))
.minimumScaleFactor(0.75)
.padding([.bottom], 4)
HStack(spacing: 8){
Text(article.dateText)
.font(Font.custom("Inter-Regular.ttf", size: 12))
Image("rect")
Text(article.tickerText)
.font(Font.custom("Inter-Regular.ttf", size: 12))
Image("green")
.padding([.leading, .trailing], -8)
Text("2.33%")
.font(Font.custom("Inter-Regular.ttf", size: 12))
.foregroundColor(Color(red: 0.345, green: 0.50, blue: 0.155, opacity: 1.0))
}.lineLimit(1)
.minimumScaleFactor(0.75)
}
AsyncImage(url: article.imageURL) {image in
image.resizable()
.frame(width: 64, height: 64, alignment: .trailing)
.aspectRatio(contentMode: .fill)
.cornerRadius(6)
} placeholder: {
ProgressView()
}
}.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
.listRowSeparator(.hidden)
Image("rect-hor")
}
}
}
在 VStack
和 AsyncImage
之间使用 Spacer()
,这将填满 HStack
中可用的水平 space,并将您的图像推送到后缘:
VStack(alignment: .leading, spacing: 4) {
//content
}
Spacer() //<-- Here
AsyncImage(url: article.imageURL) { image in
image.resizable()
.frame(width: 64, height: 64)
.aspectRatio(contentMode: .fill)
.cornerRadius(6)
} placeholder: {
ProgressView()
}