我有一个SwiftUI VStack,它位于一个scrollView,一个几何阅读器和一个NavigationView中,代码如下:
struct RezeptList: View {
@Environment(\.colorScheme) var colorScheme: ColorScheme
@EnvironmentObject private var recipeStore: RecipeStore
@State private var searching = false
@State private var searchText = ""
@State private var showingAddRecipeView = false
var body: some View {
NavigationView{
GeometryReader { geo in
ScrollView {
SearchBar(searchText: self.$searchText, isSearching: self.$searching)
VStack(spacing: 30) {
ForEach(self.recipeStore.recipes.filter{$0.name.hasPrefix(self.searchText) || self.searchText == ""}) {recipe in
NavigationLink(destination:
RezeptDetail(recipe: recipe).environmentObject(self.recipeStore)) {
Card(rezept: recipe,width: geo.size.width - 20)
}
.buttonStyle(PlainButtonStyle())
}
.navigationBarItems(trailing: Button(action: {
self.showingAddRecipeView = true
}){
Image(systemName: "square.and.pencil")
.foregroundColor(.primary)
}
.padding()
)
}
.padding(.bottom)
.navigationBarTitle("Rezepte")
.sheet(isPresented: self.$showingAddRecipeView) {
AddRecipeView(isPresented: self.$showingAddRecipeView)
.environmentObject(self.recipeStore)
}
}
}
}
}
init() {
UINavigationBar.appearance().tintColor = UIColor.label
}
}但是不管间距有多大,它看起来都是这样的:Image
但我注意到,当我移动.navigationBarItems修饰符时,它可以工作,但当你点击navigationLink时,应用程序就崩溃了。
发布于 2020-04-11 05:33:28
SwiftUI有时会在放置一些修饰符时出现奇怪的行为问题。
在你的例子中,如果你将.navigationBarItems移到navigationBarTitle之后,它应该会解决这个问题,你会得到你的VStack间距。
.navigationBarTitle("Rezepte")
.navigationBarItems(trailing: Button(action: {
self.showingAddRecipeView = true
}, label: {
Image(systemName: "square.and.pencil")
.foregroundColor(.primary)
}).padding())此外,我观察到,让这些与导航相关的修饰符更接近NavigationView要比深入层次结构更好。
示例(基于视图层次结构):
struct ContentView: View {
@State var isShowing: Bool = false
var body: some View {
NavigationView {
GeometryReader { (geo) in
ScrollView {
VStack(spacing: 60) {
ForEach(0...10, id:\.self) { (index) in
NavigationLink(destination: Text(String(index))) {
Text("Button")
}
}
}
.navigationBarTitle("Title")
.navigationBarItems(trailing: Button(action: {
self.isShowing = true
}, label: {
Image(systemName: "square.and.pencil")
}))
.sheet(isPresented: self.$isShowing) {
Button(action: {
self.isShowing = false
}) {
Text("Dismiss")
}
}
}
}
}
}
}https://stackoverflow.com/questions/61148593
复制相似问题