我正在尝试在 SwiftUI 中创建一个视图。在预览中,它看起来应该是这样,但是当在我的 iPhone 上(或实时预览)上运行时,它看起来像是偏移了。
我尝试将填充设置为 -150,但 TextField 不响应触摸。
VStack {
Text("Name:")
.padding(.bottom, 1)
TextField($name)
.padding(.horizontal, 25.0)
.textFieldStyle(.roundedBorder)
.frame(maxWidth: 500)
Text("Image:")
.padding(.top, 1)
Image(uiImage: image!)
.resizable(capInsets: EdgeInsets(), resizingMode: .stretch)
.scaledToFit()
.frame(width: 250, height: 250)
.clipShape(RoundedRectangle(cornerRadius: 10))
.padding(.top, 5)
Button(action: {
withAnimation {
self.showImagePicker = true
}
}) {
Text("Select Image")
.color(.init(red: 20/255, green: 146/255, blue: 81/255))
}
Button(action: {
let list = LSList( title: self.name,
image: self.image!,
id: 0)
list.add()
self.userData.listsData.append(list)
}) {
Text("Add List")
.color(.white)
.font(.system(size: 25))
.bold()
.padding(.horizontal, 7)
.frame(height: 35)
.background(Color.green)
.clipShape(RoundedRectangle(cornerRadius: 3))
}
Spacer()
} .navigationBarTitle(Text("Add List"))
The view in the preview:
![The view in the preview](https://i.stack.imgur.com/TTwOx.png)
The view on my iPhone:
![The view on my iPhone](https://i.stack.imgur.com/4KW5Y.jpg)
我有同样的问题,但发现我遇到的问题是多次使用 navigationView 。
我认为我们应该在每个视图中都有NavigationView,但显然,我们应该只将navigationView放置在应用程序的主视图中,而我们通过navigationLink进入的所有其他视图都会自动获得后退选项,而无需再次提及NavigationView。
因此,请检查您是否在代码中多次使用 navigationView 。
奇怪的是,即使在没有提到 navigationView 的视图中,我们也可以指定 navigationBarTitle,这是因为 navigationView 位于父视图中。
使用“displayMode: .inline”,使导航区域尽可能小,这会节省您的空间。
另一件要做的事情是使用 Spacer()。
如果您希望所有项目都位于顶部,只需将 Spacer() 作为 VStack 中的最后一个项目,它会将所有项目推到顶部。
看这个例子:
VStack {
// What you care about displaying
Text("Something to Show 1")
Text("Something to Show 2")
Text("Something to Show 3")
// This should be the last, put everything to the top
Spacer()
}
.navigationBarTitle(Text("The Title"), displayMode: .inline)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)