我从一个干净的项目开始,在 VStack 中添加了 5 个按钮和 5 个垫片,一切都很好。当我在底部添加第 6 个间隔时,代码突然无法编译并出现错误:“对成员'buildBlock()'的不明确引用”。
是什么导致了这个错误?这是与 SwiftUI 相关的错误吗?或者它是一个功能?这不是我第一次注意到 VStack 或 HStack 的条目数量有限,是否有相关文档?
不完全鼓舞人心,我应该切换回 UIKit 吗?
如何在 VStack 中获得空间来将按钮固定在底部?
ScrollView {
VStack() {
Text("Title_1")
.padding(.bottom, 35.0)
Text("Title_2")
.padding(.bottom, 32.0)
Text("Title_3")
.padding(.bottom, 27.0)
Spacer()
Button(action: { print("ACTION") }) {
Text("OK")
.font(.title)
.fontWeight(.semibold)
.foregroundColor(Color.red)
}
.frame(height: 35)
.cornerRadius(8.0)
.padding(.bottom, 25.0)
}
.frame(maxWidth: .infinity)
}
Run Code Online (Sandbox Code Playgroud)
目前正在构建一个聊天应用程序,我需要在屏幕底部显示新消息。我还需要将消息对齐到底部。但是,在 ScrollView 中使用 VStack 默认情况下具有顶部对齐。

ScrollView {
VStack(alignment: .leading, spacing: 16) {
Spacer()
.frame(minWidth: 0, maxWidth: .infinity, minHeight:0, maxHeight: .infinity, alignment: Alignment.topLeading)
ForEach(notList, id: \.self) { not in
NotRow(not: not)
}
}
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight:0, alignment: Alignment.topLeading)
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
我正在尝试使用 SwiftUI 重新创建基本的集合视图行为:
我有许多水平显示的视图(例如照片)。当没有足够的空间在同一行显示所有照片时,剩余的照片应换行到下一行。
下面是一个例子:
看起来可以将一个元素VStack与多个HStack元素一起使用,每个元素包含一行的照片。
我尝试使用GeometryReader和迭代照片视图来动态创建这样的布局,但它不会编译(包含声明的闭包不能与函数构建器 'ViewBuilder' 一起使用)。是否可以动态创建视图并返回它们?
澄清:
盒子/照片可以有不同的宽度(与经典的“网格”不同)。棘手的部分是我必须知道当前框的宽度才能决定它是否适合当前行,或者我是否必须开始新行。
我希望在屏幕垂直中心有一个视图,在屏幕顶部有一个视图,在这两个视图之间垂直居中有一个视图,如下所示:
这花了我 5 分钟在故事板上完成,但我似乎找不到在 SwiftUI 中完成它的方法。
我已经尝试过多个 zstack、vstack、多个自定义对齐,但这是我得到的最接近的:
struct SelectionView: View {
var body: some View {
ZStack(alignment: .myAlignment) {
Color.green
.edgesIgnoringSafeArea(.all)
VStack {
Image(systemName: "clock")
.resizable()
.foregroundColor(.white)
.aspectRatio(contentMode: .fit)
.frame(width: 156, height: 80)
// Spacer()
Text("My\nmultiline label")
.multilineTextAlignment(.center)
.font(.title)
.foregroundColor(.white)
// Spacer()
VStack(spacing: 16) {
RoundedRectangle(cornerRadius: 5).fill(Color.white).frame(height: 79)
RoundedRectangle(cornerRadius: 5).fill(Color.white).frame(height: 79)
}
.alignmentGuide(VerticalAlignment.myAlignment) { dimension in
dimension[VerticalAlignment.center]
}
.layoutPriority(1)
}
.padding([.leading, .trailing], 24)
}
}
}
struct SelectionView_Previews: PreviewProvider {
static var previews: some View {
LanguageSelectionView()
}
} …Run Code Online (Sandbox Code Playgroud) 我想将 a 的内容左对齐VStack而不是居中,但我不知道该怎么做。这是我的代码:
struct SortRow: View {
var sortType: SortType
@AppStorage(sortKey) var currentSorting: SortType = .winOrLoss
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text(sortType.name)
.multilineTextAlignment(.leading)
.font(.system(size: 15.0))
.foregroundColor(Color(sortType == currentSorting ? UIColor.white : UIColor.systemGray2))
Text(sortType.detail)
.multilineTextAlignment(.leading)
.font(.system(size: 13.0))
.foregroundColor(Color(sortType == currentSorting ? UIColor.white : UIColor.systemGray))
}
.frame(maxWidth: .infinity)
.padding(12)
.background(Color(sortType == currentSorting ? UIColor.systemGreen : UIColor.systemBackground))
}
}
Run Code Online (Sandbox Code Playgroud)
我得到什么:
为什么会有这样的左右填充来使文本内容居中?
感谢您的帮助
有没有更好的方法在 SwiftUI 中定位文本,在下面的示例中,我将文本定位在 ZStack 的右下角,它工作正常但似乎冗长,我是否缺少更简单的方法......橙色线是仅用于调试,以便间隔在视图中可见。
代码
struct DisplayTwoView: View {
var body: some View {
ZStack {
Rectangle().foregroundColor(.blue)
Group {
VStack {
Spacer().frame(width: 5).background(Color.orange)
HStack {
Spacer().frame(height: 5).background(Color.orange)
Text("RABBITS").fontWeight(.black)
}
}
}.padding()
}
}
}
Run Code Online (Sandbox Code Playgroud)
看法
我添加了spacer(minLength:5),但是我可以指定 minlenght 来指定文本之间的间距。我已经附上了一个屏幕截图,以供参考,我想减少内部hstack之间的间距。
HStack {
Image("Rhea").resizable().cornerRadius(25).frame(width: 50.0, height: 50.0)
VStack(alignment: .leading) {
Text("How to enjoy your life without money").bold().font(.system(size: 20))
HStack {
Text("Lets create")
Spacer(minLength: 5)
Text("3K views")
Spacer(minLength: 5)
Text("3 hours ago")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法摆脱我的 VStack 中的这个“前导”填充。
我将其父 VStack 的对齐方式设置为“前导”,但它仍然在左侧显示一些间距(对于文本和红色圆角矩形)。它们应该放置在屏幕截图中蓝色边缘旁边的左侧。
关于为什么这个填充出现在那里有什么想法吗?
这是代码:
import SwiftUI
struct RegisterView: View {
var body: some View {
VStack(spacing: 0) {
HStack(alignment: .top) {
Text("Register")
.font(.custom("NotoSans-Regular", size: 24))
.fontWeight(.bold)
.foregroundColor(Color.tertiaryTitleColor)
}
.frame(width: 299, height: 39)
.padding(.top, 78)
HStack(spacing: 13) {
BroviderButton(imageName: "googleLogo")
BroviderButton(imageName: "facebookLogo")
}
.padding(.top, 47)
VStack(alignment: .leading, spacing: 0) {
Text("Register with E-mail")
.font(.custom("NotoSans-Regular", size: 16))
RoundedRectangle(cornerRadius: 25)
.fill(Color.red)
.frame(width: 40, height: 20)
}
.frame(width: 279)
.padding(.top, 61)
Spacer()
}
.frame(maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)
.frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)
.background(Color.loginBackgroundColor)
.ignoresSafeArea()
} …Run Code Online (Sandbox Code Playgroud) 我正在VStack显示图像(与json相比,它们将具有不同的大小)
我需要显示它以占据屏幕宽度(vstack的宽度并保持宽高比)并适当调整其大小,同时注意根据屏幕宽度的高度。我尝试了不同的方法,但是我设法正确显示了图像。
我的看法是:
struct ContentView: View {
var body: some View {
VStack {
GeometryReader { geometry in
VStack {
Text("Width: \(geometry.size.width)")
Text("Height: \(geometry.size.height)")
}
.foregroundColor(.white)
}
.padding()
.frame(alignment: .topLeading)
.foregroundColor(Color.white) .background(RoundedRectangle(cornerRadius: 10) .foregroundColor(.blue))
.padding()
GeometryReader { geometry in
VStack {
Image("sample")
.resizable()
//.frame(width: geometry.size.width)
.aspectRatio(contentMode: .fit)
}
.foregroundColor(.white)
}
.frame(alignment: .topLeading)
.foregroundColor(Color.white) .background(RoundedRectangle(cornerRadius: 10) .foregroundColor(.blue))
.padding()
}
.font(.title)
}
}
Run Code Online (Sandbox Code Playgroud)
当我.frame (width: geometry.size.width)通过分配的宽度使用时geometry,该宽度会显示在整个屏幕上,但高度不能保持纵横比。(看起来被压碎了)
如何获取图像的尺寸并找到其比例以用于 .aspectRatio (myratio, contentMode: .fit)
还有另一种正确显示图像的方法,任何建议