导航栏按钮有时无法点击

时间:2020-04-25 13:42:00

标签: swift swiftui

我为test设置了navigationBarItem按钮,以将FavoritesView显示为模式表。但是,此按钮和工作表中的SettingsView按钮都仅注册并响应某些轻击。我看不到按钮何时响应的任何模式,但有时可能需要4或5次点击才能使应用程序响应!有什么办法可以解决此问题?

Done

FavoritesView

import SwiftUI enum ActiveSheet { case details, settings } struct FavoritesView: View { let speakers: [Speaker] = Bundle.main.decode("SpeakerTestData.json") @State private var selectedModel: Speaker? @State private var showingSheet = false @State private var activeSheet: ActiveSheet = .settings @EnvironmentObject var favorites: Favorites @EnvironmentObject var settings: UserSettings var filteredFavorites: [Speaker] { let allSpeakers = speakers var filteredItems: [Speaker] = [] for entry in allSpeakers { if favorites.contains(entry) { filteredItems.append(entry) } } let sorted = filteredItems.sorted { $0.model.localizedStandardCompare($1.model) == .orderedAscending } return sorted } var body: some View { NavigationView { List { if filteredFavorites.count == 0 { Text("Items you favourite will appear here.") .foregroundColor(.secondary).padding(5) } else { Section(header: Text("Speakers")) { ForEach(filteredFavorites) { speaker in HStack { Button(action: { self.activeSheet = .details self.selectedModel = speaker self.showingSheet = true }) { SpeakerModelRow(speaker: speaker).contentShape(Rectangle()) } .buttonStyle(PlainButtonStyle()) Spacer() Button(action: { if self.favorites.contains(speaker) { self.favorites.remove(speaker) } else { self.favorites.add(speaker) } }, label: { if self.favorites.contains(speaker) { Image(systemName: "star.fill") .foregroundColor(.blue) .font(Font.title.weight(.ultraLight)) } else { Image(systemName: "star") .foregroundColor(.gray) .font(Font.title.weight(.ultraLight)) } } ).padding(5) } } } } } .navigationBarTitle("Favourites") .navigationBarItems(trailing: Button(action: { self.activeSheet = .settings self.showingSheet = true }){ Image(systemName: "gear").font(Font.title.weight(.ultraLight)).padding(.trailing, 5).foregroundColor(.primary) }) .sheet(isPresented: self.$showingSheet) { if self.activeSheet == .details { SpeakerDetailView(speaker: self.selectedModel!, showSheet: self.$showingSheet).environmentObject(self.favorites).environmentObject(self.settings) } else { SettingsView(showSheet: self.$showingSheet).environmentObject(self.settings) } } } } } 列为表格:

SettingsView

1 个答案:

答案 0 :(得分:1)

这是一个已知问题,请尝试使用内部按钮内容填充(默认值或更多),例如

.navigationBarItems(leading: Button(action: {
    self.showSheet = false
}) { Text("Done").padding() }