Swift @环境(\.presentationMode)变量模式:绑定<;呈现模式>;扰乱其他观点

Swift @环境(\.presentationMode)变量模式:绑定<;呈现模式>;扰乱其他观点,swift,swiftui,Swift,Swiftui,我有一个MailView() 有一个行动表: .actionSheet(isPresented: self.$profileViewModel.showActionSheet){ ActionSheet(title: Text("Add a profile image"), message: nil, buttons: [ .default(Text("Camera"), action: {

我有一个MailView()

有一个行动表:

.actionSheet(isPresented: self.$profileViewModel.showActionSheet){
        ActionSheet(title: Text("Add a profile image"), message: nil, buttons: [
            .default(Text("Camera"), action: {
                self.profileViewModel.showImagePicker = true
                self.sourceType = .camera
            }),
            .default(Text("Photo Library"), action: {
                self.profileViewModel.showImagePicker = true
                self.sourceType = .photoLibrary
            }),
            .cancel()
            
        ])
    }.sheet(isPresented: self.$profileViewModel.showImagePicker){
        imagePicker(image: self.$profileViewModel.upload_image, showImagePicker: self.$profileViewModel.showImagePicker, sourceType: self.sourceType)
    }
当我点击这个按钮时,它会一直关闭按钮,我无法点击显示的选项

你知道我如何让
@Environment(\.presentationMode)var模式:Binding
只影响邮件的发送吗?并且不干扰任何其他内容?

@Environment(\.presentationMode)
应用于您希望具有此自定义行为的最后一个子视图

从中声明
@环境(\.presentationMode)
的任何子视图也将继承相同的行为


如果您仅在
邮件视图中声明它,它应该会修复它。

删除上面的行,即使在我将其放置在邮件视图中,也会导致该工作表根本不显示。我想我仍然需要它在SettingView中,因为它在SettingView中显示了带有MailView的工作表。因此,这确实解决了问题,我忘记了在电子邮件中,它只在真实设备上工作,而不在模拟器上工作。
import SwiftUI
import URLImage
import UIKit
import MessageUI

struct SettingsView: View {

@Environment(\.presentationMode) var mode: Binding<PresentationMode>

@State private var showMailSheet = false
@State var result: Result<MFMailComposeResult, Error>? = nil

@State private var subject: String = ""
@State private var emailBody: String = ""

@EnvironmentObject var session: SessionStore

var body: some View {
    
    NavigationView {
        VStack(alignment: .leading) {
            List {
                Section(header: Text("Account")) {
                    NavigationLink(destination: ProfileView()) {
                        HStack {
                            Image(systemName: "person")
                                .resizable()
                                .frame(width: 20, height: 20)
                            VStack(alignment: .leading) {
                                Text("Edit Profile").font(.callout).fontWeight(.medium)
                            }
                        }.padding([.top,.bottom],5).padding(.trailing,10)
                    }
                    NavigationLink(destination: AccountView()) {
                        HStack {
                            Image(systemName: "doc")
                                .resizable()
                                .frame(width: 20, height: 20)
                            VStack(alignment: .leading) {
                                Text("View Account").font(.callout).fontWeight(.medium)
                            }
                        }.padding([.top,.bottom],5).padding(.trailing,10)
                    }
                    NavigationLink(destination: PreferencesView()) {
                        HStack {
                            Image(systemName: "slider.horizontal.3")
                                .resizable()
                                .frame(width: 20, height: 20)
                            VStack(alignment: .leading) {
                                Text("Preferences").font(.callout).fontWeight(.medium)
                            }
                        }.padding([.top,.bottom],5).padding(.trailing,10)
                    }
                }
                Section(header: Text("Support")) {
                    HStack {
                        Image(systemName: "bubble.right")
                            .resizable()
                            .frame(width: 20, height: 20)
                        VStack(alignment: .leading) {
                            Text("Contact Us").font(.callout).fontWeight(.medium)
                        }
                        Spacer()
                        Button(action: {
                            self.subject = "Hello"
                            self.sendEmail()
                        }) {
                            Text("Send").font(.system(size:12))
                        }
                    }
                    HStack {
                        Image(systemName: "ant")
                            .resizable()
                            .frame(width: 20, height: 20)
                        VStack(alignment: .leading) {
                            Text("Report An Issue").font(.callout).fontWeight(.medium)
                        }
                        Spacer()
                        Button(action: {
                            self.sendEmail()
                            self.subject = "Report Issue"
                            self.emailBody = "Im having the following issues:"
                        }) {
                            Text("Report").font(.system(size:12))
                        }
                    }
                }
                Section (header: Text("Legal")) {
                    HStack {
                        Image(systemName: "hand.raised")
                            .resizable()
                            .frame(width: 20, height: 20)
                        VStack(alignment: .leading) {
                            Text("Privacy Policy").font(.callout).fontWeight(.medium)
                        }
                        Spacer()
                        Button(action: {
                            if let url = URL(string: "http://www.mysite.co.uk/privacy.html") {
                               UIApplication.shared.open(url)
                           }
                        }) {
                            Text("View").font(.system(size:12))
                        }
                    }
                    HStack {
                        Image(systemName: "folder")
                            .resizable()
                            .frame(width: 20, height: 20)
                        VStack(alignment: .leading) {
                            Text("Terms and Conditions (EULA)").font(.callout).fontWeight(.medium)
                        }
                        Spacer()
                        Button(action: {
                            if let url = URL(string: "http://www.mysite.co.uk/eula.html") {
                               UIApplication.shared.open(url)
                           }
                        }) {
                            Text("View").font(.system(size:12))
                        }
                    }
                }
            }.listStyle(GroupedListStyle())
        }.navigationBarTitle("Settings", displayMode: .inline)
        .background(NavigationBarConfigurator())
    }.sheet(isPresented: $showMailSheet) {
        MailView(result: self.$result, newSubject: self.subject, newMsgBody: self.emailBody)
    }
}

func sendEmail() {
    if MFMailComposeViewController.canSendMail() {
        self.showMailSheet = true
    } else {
        print("Error sending mail")
    }
}
}

struct SettingsView_Previews: PreviewProvider {
    static var previews: some View {
    SettingsView()
    }
}
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
                    NavigationLink(destination: ProfileView()) {
                        HStack {
                            Image(systemName: "person")
                                .resizable()
                                .frame(width: 20, height: 20)
                            VStack(alignment: .leading) {
                                Text("Edit Profile").font(.callout).fontWeight(.medium)
                            }
                        }.padding([.top,.bottom],5).padding(.trailing,10)
                    }
.actionSheet(isPresented: self.$profileViewModel.showActionSheet){
        ActionSheet(title: Text("Add a profile image"), message: nil, buttons: [
            .default(Text("Camera"), action: {
                self.profileViewModel.showImagePicker = true
                self.sourceType = .camera
            }),
            .default(Text("Photo Library"), action: {
                self.profileViewModel.showImagePicker = true
                self.sourceType = .photoLibrary
            }),
            .cancel()
            
        ])
    }.sheet(isPresented: self.$profileViewModel.showImagePicker){
        imagePicker(image: self.$profileViewModel.upload_image, showImagePicker: self.$profileViewModel.showImagePicker, sourceType: self.sourceType)
    }