Swiftui 工作表关闭后导航栏按钮不工作。航空包装?
我正在开发一个SwiftUI应用程序,我有一个Swiftui 工作表关闭后导航栏按钮不工作。航空包装?,swiftui,swiftui-navigationview,Swiftui,Swiftui Navigationview,我正在开发一个SwiftUI应用程序,我有一个NavigationView,导航栏上有一些按钮。问题是,在关闭一张完整的页面后(由按钮触发,而不是在导航栏中),这些按钮停止工作 我尝试了以下方法,但一些TestFlight用户表示问题仍然存在(我自己无法重现): 为每个按钮添加一个id,并在关闭后更改它(我甚至将其添加到工具栏),以强制重新绘制 为按钮和导航栏添加高度 将@Environment(\.presentationMode)var presentationMode:Binding添加到
NavigationView
,导航栏上有一些按钮。问题是,在关闭一张完整的页面后(由按钮触发,而不是在导航栏中),这些按钮停止工作
我尝试了以下方法,但一些TestFlight用户表示问题仍然存在(我自己无法重现):
@Environment(\.presentationMode)var presentationMode:Binding
添加到呈现视图和呈现视图导航项
?或者只是按钮。答案没有详细说明
只有当工作表通过导航栏外的按钮显示时,才会出现这种情况。导航栏中的按钮也显示图纸,不会引起任何问题。我很想把导航栏完全隐藏起来,然后用常规视图来伪装它
为了防止您想看到我所拥有的内容,以下是我演示视图中的相关代码(我删除了一些不相关的内容和功能):
struct PListView:View{
//https://stackoverflow.com/questions/58837007/multiple-sheetispresented-doesnt-work-in-swiftui
枚举ActiveSheetProjectList:可识别{
案例帮助,设置
变量id:Int{
哈希值
}
}
枚举ActiveFullSheetProjectList:可识别{
case addProject,quickCount
变量id:Int{
哈希值
}
}
@观察对象变量视图模型:ProjectListViewModel
@状态变量presentingdeletprojectsheet=false
@状态变量itemsToDelete:[UUID]?
@状态变量activeSheet:ActiveSheetProjectList?
@状态变量activeFullSheet:ActiveFullSheetProjectList?
@ObservedObject变量设置:设置
@环境(\.presentationMode)变量presentationMode:绑定
初始化(设置:设置){
self.viewModel=ProjectListViewModel()
self.settings=设置
//https://medium.com/@francisco.gindre/customization-swiftui-navigation-bar-8369d42b8805
//这与直接操作代理不同
让外观=UINavigationBarAppearance()
//这将覆盖您先前设置的所有内容。
外观。配置为透明背景()
appearance.backgroundColor=UIColor(Color.navBar)
//这只适用于大标题
外观.largeTitleTextAttributes=[
.font:UIFont.systemFont(大小:20),
NSAttributeString.Key.foregroundColor:UIColor(Color.smallTextMain)
]
//这只适用于小标题
外观.titleTextAttributes=[
.font:UIFont.systemFont(大小:20),
NSAttributeString.Key.foregroundColor:UIColor(Color.smallTextMain)
]
//在下面的两行中,请确保应用了正确的样式
UINavigationBar.appearance().scrollEdgeAppearance=外观
UINavigationBar.appearance().standardAppearance=外观
//UINavigationBarAppearance上不存在此属性
//由于某种原因,您必须将其保留到最后
UINavigationBar.appearance().tintColor=UIColor(Color.smallTextMain)
}
var body:一些观点{
导航视图{
ZStack{
Color.background.edgesIgnoringSafeArea(.all)
VStack{
名单{
ForEach(projects){projectin
NavigationLink(目标:ProjectView(项目:项目,设置:设置,viewModel:ProjectListViewModel(),viewContext:viewContext)
.environmentObject(自我设置)
{
HStack{
文本(project.name???).font(.headline).填充(.bottom,5).填充(.top,5)
}
}
.listRowInsets(.init(顶部:10,前导:3,底部:10,尾随:3))
.accessibilityHint(文本(NSLocalizedString(“查看项目详细信息”,注释:))
}
.onDelete(执行:{indexSet in
presentingDeleteProjectSheet=true
itemsToDelete=indexSet.map{projects[$0].id!}
})
.listRowBackground(颜色为浅灰色)
.填充(0)
.actionSheet(显示:$presentingDeleteProjectSheet){
var name=NSLocalizedString(“项目”,注释:“通用标签”)
如果let id=itemsToDelete?。首先{
name=projects.first(其中:{$0.id==id})?.name??“
}
返回操作表(标题:文本(NSLocalizedString(String.localizedStringWithFormat(“Delete%@”,name),注释:“警报标题”)),消息:文本(NSLocalizedString(“删除项目无法撤消”,注释:“删除警报消息”)),按钮:[
.破坏性(文本(NSLocalizedString(“删除”,注释:“按钮标签”)){
如果itemsToDelete!=ni
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)
}
}
}
}
}