我有一个带有VStack的body,另一个VStack,我想从20点开始,就像我的“探索更多”文本一样,但由于某些原因,它像这样缩进,我不知道为什么。希望能帮上忙。
struct BrandExploreMore: View {
let brand: Brand
var body: some View {
VStack {
HStack {
Text("Explore More")
.font(.headline)
.foregroundColor(BrandColors.titleGray.color)
.padding(.leading, 20)
Spacer()
}
VStack(spacing: 12) {
Grid(leftTitle: "Desert", rightTitle: "Kids")
Grid(leftTitle: "Stripes", rightTitle: "Pastels")
}
//.padding(.horizontal, 20)
// .padding(EdgeInsets(top: 0, leading: -40, bottom: 0, trailing: 0))
.padding(.bottom, 20)
.background(SwiftUI.Color.red)
}.background(SwiftUI.Color.orange) // end VStack
}
}
struct BrandExploreMore_Previews: PreviewProvider {
static var previews: some View {
BrandExploreMore(brand: .kateZaremba)
}
}
// MARK: - Grid
struct Grid: View {
let leftTitle: String
let rightTitle: String
@State private var showLeft = false
@State private var showRight = false
var body: some View {
HStack(spacing: 12) {
// Spacer()
Button(action: { self.showLeft = true }) {
ZStack {
Image(leftTitle)
Text(leftTitle)
.foregroundColor(BrandColors.titleGray.color)
.font(.subheadline)
}
}.sheet(isPresented: self.$showLeft) {
Text(self.leftTitle)
}
Button(action: { self.showRight = true }) {
ZStack {
Image(rightTitle)
Text(rightTitle)
.foregroundColor(BrandColors.titleGray.color)
.font(.subheadline)
}
}.sheet(isPresented: self.$showRight) {
Text(self.rightTitle)
}
// Spacer()
}
}
}

发布于 2019-11-01 04:18:03
对于需要拉伸以填充的任何内容,您需要的修饰符是.frame(maxWidth: .infinity)。
在你的代码中:
在BrandExploreMore中的Grid
Button中的VStack(spacing: 12)因此,在删除未知资源、清理代码并对其执行一些重构之后:
struct BrandExploreMore: View {
var body: some View {
VStack(alignment: .leading) {
Group {
Text("Explore More")
VStack(spacing: 12) {
Grid(leftTitle: "Desert", rightTitle: "Kids")
Grid(leftTitle: "Stripes", rightTitle: "Pastels")
}
.frame(maxWidth: .infinity)
.padding(.bottom, 20)
.background(SwiftUI.Color.red)
}.padding(.horizontal, 12)
}
.background(SwiftUI.Color.orange) // end VStack
}
}
struct BrandExploreMore_Previews: PreviewProvider {
static var previews: some View {
BrandExploreMore()
}
}
// MARK: - Grid
struct Grid: View {
let leftTitle: String
let rightTitle: String
@State private var showLeft = false
@State private var showRight = false
var body: some View {
HStack(spacing: 12) {
Button(action: { self.showLeft = true }) {
ZStack {
Rectangle()
.foregroundColor(.yellow)
.frame(maxHeight: 100)
.cornerRadius(16)
Text(leftTitle)
.foregroundColor(.blue)
.font(.subheadline)
}
}.sheet(isPresented: self.$showLeft) {
Text(self.leftTitle)
}
.frame(maxWidth: .infinity)
Button(action: { self.showRight = true }) {
ZStack {
Rectangle()
.foregroundColor(.yellow)
.frame(maxHeight: 100)
.cornerRadius(16)
Text(rightTitle)
.foregroundColor(.black)
.font(.subheadline)
}
}.sheet(isPresented: self.$showRight) {
Text(self.rightTitle)
}
.frame(maxWidth: .infinity)
}
}
}结果是:

https://stackoverflow.com/questions/58649912
复制相似问题