我创建了一个ContentView()页面,它显示了应用程序的主要内容。在添加@Environment(\.presentationMode) var presentationMode
和有关presentationMode的函数之前,尾随部分的工作表视图是正常工作的,没有任何问题。现在,主页按钮(引导)工作良好,并允许我返回到应用程序的菜单页面,但单击另一个按钮(尾随)崩溃的应用程序。奇怪的是,ContentView()本身在预览模式下运行时没有任何问题,但却使模拟器崩溃。我试过很多事情,但找不到解决这个问题的正确方法。
提前谢谢。
这是ContentView()的一部分:
import SwiftUI
struct ContentView: View {
@State var isAddPresented = false
@State var isActive : Bool = false
@Environment(\.presentationMode) var presentationMode
func goBack(){
self.presentationMode.wrappedValue.dismiss()
}
var body: some View {
NavigationView{
VStack{
HStack{
Text("")
.navigationViewStyle(StackNavigationViewStyle())
.navigationBarItems(leading:
Button(action: goBack) {
Image("Home") },trailing:
Button(action: {
self.isAddPresented = true
}) {
Image("Rules_Click")
}).padding()
}.sheet(isPresented: $isAddPresented,
onDismiss: { self.isAddPresented = false })
{RulesView()}.padding(.bottom, 100.0)
}
}
这是HomeView()的一部分:
HStack{
NavigationLink(destination: ContentView()){
Image("NormalCards")
.resizable(resizingMode: .stretch)
.aspectRatio(contentMode: .fit)
.navigationTitle("")
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
}
下面是RulesView部分:
import SwiftUI
struct RulesView: View {
var body: some View {
Image("Rules")
.resizable(resizingMode: .stretch)
.aspectRatio(contentMode: .fill)
}
}
struct Rules_Previews: PreviewProvider {
static var previews: some View {
RulesView()
}
}
编辑:
将.navigationBarItems更改为工具栏并没有改变任何事情。
.toolbar{
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
goBack()
}, label: {
Image("Home")
})
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.isAddPresented = true
}, label: {
Image("Rules_Click")
}).sheet(isPresented: $isAddPresented, onDismiss: { self.isAddPresented = false }) {RulesView()}
}
}
编辑Nr.2:
在这里,使用文本(“ExampleText”)代替ZStack可以很好地工作。
struct ContentView: View {
@Environment(\.dismiss) var dismiss
@State private var isAddPresented = false
func goBack(){
dismiss() // reset to home view
}
var body: some View {
ZStack(alignment:.center) {
ForEach(Card.data) { card in
CardView(card: card)
}
}
.navigationBarBackButtonHidden(true)
.toolbar{
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
goBack()
}, label: {
Image("Home")
})
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.isAddPresented = true
}, label: {
Image("Rules_Click")
})
}
}
.sheet(isPresented: $isAddPresented) {
RulesView()
}
}
}
在这里,您可以找到有关CardView() ZStack的信息:
struct CardView: View {
@State var card: Card
var body: some View {
ZStack {
Image(card.imageName)
}
//.background(brownish)
.offset(x: card.x, y: card.y)
.rotationEffect(.init(degrees: card.deg))
.gesture(
DragGesture()
.onChanged { value in
withAnimation(.default){
card.x = value.translation.width
card.y = value.translation.height
card.deg = 7*(value.translation.width > 0 ? 1 : -1)
}
}
.onEnded { value in
withAnimation(.interpolatingSpring(mass: 1.0, stiffness: 50, damping: 8, initialVelocity: 0)) {
switch value.translation.width {
case 0...100:
card.x = 0; card.deg = 0; card.y = 0
case let x where x > 100:
card.x = 500; card.deg = 12
case (-100)...(-1):
card.x = 0; card.deg = 0; card.y = 0;
case let x where x < -100:
card.x = -500; card.deg = -12
default: card.x = 0; card.y = 0
}
}
}
)
}
}
发布于 2022-03-14 14:31:32
主要是您应该将.sheet
从.toolbar
中提取出来。
其次,每个模态视图都有自己的dismiss
,因此这两个视图(NavigationLink和Sheet)可能会产生干扰。
最后,您可以使用@Environment(\.dismiss) var dismiss
而不是.presentationMode
(基本上是一样的,只是更短)
我重新点了一下,然后来到这里,这对我来说很管用:
struct HomeView: View {
var body: some View {
NavigationView {
VStack{
NavigationLink {
ContentView()
} label: {
Text("NormalCards")
}
}
.navigationBarHidden(true)
}
}
}
struct ContentView: View {
@Environment(\.dismiss) var dismiss
@State private var isAddPresented = false
func goBack(){
dismiss() // reset to home view
}
var body: some View {
VStack{
HStack{
Text("Example Text")
}
}
.navigationBarBackButtonHidden(true)
.toolbar{
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
goBack()
}, label: {
Text("Home")
})
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.isAddPresented = true
}, label: {
Text("Rules")
})
}
}
.sheet(isPresented: $isAddPresented) {
RulesView()
}
}
}
struct RulesView: View {
@Environment(\.dismiss) var dismiss
var body: some View {
NavigationView {
Text("Here are the rules")
.toolbar {
Button("Close") {
dismiss()
}
}
}
}
}
https://stackoverflow.com/questions/71466556
复制相似问题