在滚动视图中调整多个 vstack 宽度,SwiftUI
Sizing multiple vstack width in scroll view, SwiftUI
我有一个 VStack
:
VStack(alignment: .leading) {
Text(question)
.fontWeight(.heavy)
.font(.system(.footnote, design: .rounded))
.padding(.bottom)
Text(answer)
.fontWeight(.semibold)
.font(.system(.title, design: .rounded))
}
.padding(35)
.overlay(RoundedRectangle(cornerRadius: 12).stroke(Color("darkGrey"), lineWidth: 2))
.padding([.leading,.top,.trailing])
.frame(minWidth: UIScreen.main.bounds.width - 5,
minHeight: 50,
maxHeight: .infinity,
alignment: .topLeading
)
我在 View
中使用它,例如:
ScrollView(.horizontal, showsIndicators: false) {
Group {
HStack {
questionCard(question: "Cats or Dogs", answer: "Dogs")
questionCard(question: "Cats or Dogs", answer: "Dogs")
}
}
}
结果如下:
我正在尝试将框的 width
移到屏幕的末尾(使用 .trailing
space),然后当我滚动到下一个时,它应该宽度相同。
您可以执行以下操作:
struct ContentView: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
Group {
HStack {
QuestionCard(question: "Cats or Dogs", answer: "Dogs")
QuestionCard(question: "Cats or Dogs", answer: "Dogs")
}
}
}
}
}
struct QuestionCard: View {
let question: String
let answer: String
var body: some View {
HStack {
Spacer()
VStack(alignment: .leading) {//<- Change the alignment if needed
Text(question)
.fontWeight(.heavy)
.font(.system(.footnote, design: .rounded))
.padding(.bottom)
Text(answer)
.fontWeight(.semibold)
.font(.system(.title, design: .rounded))
}
Spacer()
}
.padding(35)
.overlay(RoundedRectangle(cornerRadius: 12).stroke(Color.black, lineWidth: 2))
.padding([.leading, .top, .trailing])
.frame(minWidth: UIScreen.main.bounds.width - 5,
minHeight: 50,
maxHeight: .infinity,
alignment: .top
)
}
}
所以基本上我只是使用一个 HStack
和两个 Spacers
来使用所有可用的 space。
它看起来像这样:
我有一个 VStack
:
VStack(alignment: .leading) {
Text(question)
.fontWeight(.heavy)
.font(.system(.footnote, design: .rounded))
.padding(.bottom)
Text(answer)
.fontWeight(.semibold)
.font(.system(.title, design: .rounded))
}
.padding(35)
.overlay(RoundedRectangle(cornerRadius: 12).stroke(Color("darkGrey"), lineWidth: 2))
.padding([.leading,.top,.trailing])
.frame(minWidth: UIScreen.main.bounds.width - 5,
minHeight: 50,
maxHeight: .infinity,
alignment: .topLeading
)
我在 View
中使用它,例如:
ScrollView(.horizontal, showsIndicators: false) {
Group {
HStack {
questionCard(question: "Cats or Dogs", answer: "Dogs")
questionCard(question: "Cats or Dogs", answer: "Dogs")
}
}
}
结果如下:
我正在尝试将框的 width
移到屏幕的末尾(使用 .trailing
space),然后当我滚动到下一个时,它应该宽度相同。
您可以执行以下操作:
struct ContentView: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
Group {
HStack {
QuestionCard(question: "Cats or Dogs", answer: "Dogs")
QuestionCard(question: "Cats or Dogs", answer: "Dogs")
}
}
}
}
}
struct QuestionCard: View {
let question: String
let answer: String
var body: some View {
HStack {
Spacer()
VStack(alignment: .leading) {//<- Change the alignment if needed
Text(question)
.fontWeight(.heavy)
.font(.system(.footnote, design: .rounded))
.padding(.bottom)
Text(answer)
.fontWeight(.semibold)
.font(.system(.title, design: .rounded))
}
Spacer()
}
.padding(35)
.overlay(RoundedRectangle(cornerRadius: 12).stroke(Color.black, lineWidth: 2))
.padding([.leading, .top, .trailing])
.frame(minWidth: UIScreen.main.bounds.width - 5,
minHeight: 50,
maxHeight: .infinity,
alignment: .top
)
}
}
所以基本上我只是使用一个 HStack
和两个 Spacers
来使用所有可用的 space。
它看起来像这样: