我正在开发一个 SwiftUI 应用,我有一个 NavigationView
,导航栏中有一些按钮。问题是在关闭整页工作表(由按钮触发,而不是在导航栏中)后,这些按钮停止工作。
我尝试了以下方法,但一些 TestFlight 用户说问题仍然存在(我自己无法重现):
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
添加到呈现和呈现的视图我在类似帖子中看到一个答案,建议将导航包装在 UINavigation 中。但是你怎么做呢?我已经包装了视图(UITextView),但是您需要包装控制器吗?还是navigationItem
?或者只是按钮。答案没有详细说明。
只有当工作表通过导航栏外的按钮呈现时才会发生。导航栏中的按钮也显示工作表,它们不会引起任何问题。我很想完全隐藏导航栏,然后用常规视图伪造它。
以防万一你想看看我有什么,这里是我的呈现视图中的相关代码(我删除了一些不相关的内容和功能):
struct PListView: View {
//https://stackoverflow.com/questions/58837007/multiple-sheetispresented-doesnt-work-in-swiftui
enum ActiveSheetProjectList: Identifiable {
case help, settings
var id: Int {
hashValue
}
}
enum ActiveFullSheetProjectList: Identifiable {
case addProject, quickCount
var id: Int {
hashValue
}
}
@ObservedObject var viewModel : ProjectListViewModel
@State var presentingDeleteProjectSheet = false
@State var itemsToDelete : [UUID]?
@State var activeSheet: ActiveSheetProjectList?
@State var activeFullSheet : ActiveFullSheetProjectList?
@ObservedObject var settings : Settings
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
init(settings: Settings) {
self.viewModel = ProjectListViewModel()
self.settings = settings
//https://medium.com/@francisco.gindre/customizing-swiftui-navigation-bar-8369d42b8805
// this is not the same as manipulating the proxy directly
let appearance = UINavigationBarAppearance()
// this overrides everything you have set up earlier.
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor(Color.navBar)
// this only applies to big titles
appearance.largeTitleTextAttributes = [
.font : UIFont.systemFont(ofSize: 20),
NSAttributedString.Key.foregroundColor : UIColor(Color.smallTextMain)
]
// this only applies to small titles
appearance.titleTextAttributes = [
.font : UIFont.systemFont(ofSize: 20),
NSAttributedString.Key.foregroundColor : UIColor(Color.smallTextMain)
]
//In the following two lines you make sure that you apply the style for good
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().standardAppearance = appearance
// This property is not present on the UINavigationBarAppearance
// object for some reason and you have to leave it til the end
UINavigationBar.appearance().tintColor = UIColor(Color.smallTextMain)
}
var body: some View {
NavigationView {
ZStack {
Color.background.edgesIgnoringSafeArea(.all)
VStack {
List {
ForEach(projects) { project in
NavigationLink(destination: ProjectView(project: project, settings: settings, viewModel: ProjectListViewModel(), viewContext: viewContext)
.environmentObject(self.settings)
{
HStack {
Text(project.name ?? "").font(.headline).padding(.bottom, 5).padding(.top, 5)
}
}
.listRowInsets(.init(top: 10, leading: 3, bottom: 10, trailing: 3))
.accessibilityHint(Text(NSLocalizedString("View project details", comment: "")))
}
.onDelete(perform: { indexSet in
presentingDeleteProjectSheet = true
itemsToDelete = indexSet.map { projects[$0].id! }
})
.listRowBackground(Color.lightGray)
.padding(0)
.actionSheet(isPresented: $presentingDeleteProjectSheet) {
var name = NSLocalizedString("Project", comment: "Generic label")
if let id = itemsToDelete?.first {
name = projects.first(where: {$0.id == id})?.name ?? ""
}
return ActionSheet(title: Text(NSLocalizedString(String.localizedStringWithFormat("Delete %@", name), comment: "alert title")), message: Text(NSLocalizedString("Deleting a project can't be undone", comment: "Deleting alert message")), buttons: [
.destructive(Text(NSLocalizedString("Delete", comment: "Button label"))) {
if itemsToDelete != nil {
viewModel.deleteProjects(projects: activeProjectsDateCreated, ids: itemsToDelete!)
}
},
.cancel({itemsToDelete?.removeAll()})
])
}
}
.padding(0)
.onAppear(perform: {
UITableView.appearance().backgroundColor = UIColor(Color.lightGray)
UITableViewCell.appearance().selectionStyle = .none
})
}
}
}
.fullScreenCover(item: $activeFullSheet, content: { item in
switch item {
case .quickCount :
// THIS IS THE SHEET THAT CAUSES THE ISSUES
QuickCountView(viewModel: CounterViewModel(counter: viewModel.getScratchCounter(projects: quickCountProject), sound: settings.sound, showTotal: settings.showTotal, viewContext: viewContext))
.environmentObject(settings)
case .addProject:
// No problems after dismissing this one
AddEditProjectView(viewModel: AddEditProjectViewModel(project : nil, startAt: settings.startNumber, viewContext: viewContext), isNew: true, isEditing: .constant(true))
.environmentObject(settings)
}
})
Button(action: { activeFullSheet = .quickCount }, label: {
Text(NSLocalizedString("Quick Count +", comment: "Button label"))
.accessibilityLabel(NSLocalizedString("Quick count", comment: ""))
})
.buttonStyle(CustomButton(style: .button, size: .large))
.padding()
.sheet(item: $activeSheet) { item in
switch item {
case .help:
HelpView()
case .settings:
SettingsView()
.environmentObject(settings)
}
}
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
HStack {
Button(action: {
self.activeSheet = .settings
}) {
Image(systemName: "gearshape.fill")
.font(Font.system(size: 28, weight: .medium, design: .rounded))
.foregroundColor(Color.main)
.accessibilityLabel(Text(NSLocalizedString("Settings", comment: "a11y label")))
.frame(height: 96, alignment: .trailing)
}
Button(action: {
self.activeSheet = .help
}) {
Image(systemName: "questionmark")
.font(Font.system(size: 28, weight: .semibold, design: .rounded))
.foregroundColor(Color.main)
.accessibilityLabel(Text(NSLocalizedString("Help", comment: "a11y label")))
.frame(height: 96, alignment: .trailing)
}
}
}
ToolbarItemGroup(placement: .navigationBarTrailing) {
HStack {
Button(action: { activeFullSheet = .addProject }) {
Image(systemName: "plus")
.font(Font.system(size: 30, weight: .semibold))
.foregroundColor(Color.main)
.accessibilityLabel(Text(NSLocalizedString("Add a Project", comment: "a11y label")))
.frame(height: 96, alignment: .trailing)
}
Button(action: {
self.isEditing.toggle()
}) {
Image(systemName: isEditing ? "xmark" : "pencil")
.font(Font.system(size: 28, weight: .black))
.foregroundColor(activeProjectsDateCreated.count >= 1 ? Color.main : Color.gray)
.accessibilityLabel(Text(NSLocalizedString("Edit Project List", comment: "a11y label")))
.frame(height: 96, alignment: .trailing)
}.disabled(activeProjectsDateCreated.count < 1)
.frame(height: 96, alignment: .trailing)
}
}
}
}
}