在我切换到浅色或深色模式之前,SwiftUI View 不会显示数据
Posted
技术标签:
【中文标题】在我切换到浅色或深色模式之前,SwiftUI View 不会显示数据【英文标题】:SwiftUI View doesn't show data until I switch to light or dark mode 【发布时间】:2020-08-24 16:47:17 【问题描述】:这个真的把我难住了。当用户在搜索字段中键入时,我正在使用来自 ObservedViewModel 的数据加载 ScrollView/VStack。请注意,这在 ios 14 设备中完美运行,但这是我在 13.5 上遇到的行为
数据加载并且视图被刷新(所有断点都被击中)但我无法真正看到它,直到我将设备从暗模式切换到亮模式,反之亦然。 有什么想法吗?
我的搜索视图:
struct EventsSearchView: View
var searchText: String
@Environment(\.presentationMode) var presentationMode
@ObservedObject var eventsViewModel: EventsBySearchViewModel = EventsBySearchViewModel(service: EventsBySearchService())
@ObservedObject var locationManager: LocationProvider
@State var lat: String = ""
@State var long: String = ""
init(searchText: String)
self.searchText = searchText
locationManager = LocationProvider()
do
try locationManager.start()
catch
print("no location access")
locationManager.requestAuthorization()
UITableView.appearance().showsVerticalScrollIndicator = false
if !searchText.isEmpty
eventsViewModel.getEventsBySearch(keyword: searchText, latitude: lat, longitude: long)
var body: some View
ScrollView
VStack
ForEach(eventsViewModel.events, id: \.self) event in
HStack
Spacer()
CardView(event: event, lat: lat, long: long)
Spacer()
.background(Color.themeBackground)
.onAppear
if self.eventsViewModel.events.count == 0
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
if locationManager.authorizationStatus != nil
self.lat = String(locationManager.location?.coordinate.latitude ?? 0)
self.long = String(locationManager.location?.coordinate.longitude ?? 0)
locationManager.stop()
.background(Color.themeBackground)
struct EventsSearchView_Previews: PreviewProvider
static var previews: some View
EventsSearchView(searchText: "")
这是创建它的视图
struct TopView: View
@State var tabSelected: Int = 0
@State private var offset: CGFloat = 0
@State private var index = 0
@State private var showEventsSearchView: Bool = false
@State private var searchKeyword: String = ""
let spacing: CGFloat = 10
var body: some View
VStack
SearchView(showSearch: $showEventsSearchView, searchText: $searchKeyword)
if showEventsSearchView
EventsSearchView(searchText: searchKeyword)
else
【问题讨论】:
很明显,搜索时视图没有刷新...你会显示你的代码吗? 我可以在代码的所有阶段(服务、视图模型、视图)中放置断点,它们都会像这样被击中。用一些代码更新我的帖子... 再次,它在 iOS 14 上运行良好 我能够通过一个简单的视图重现这一点,其中 ScrollView/VStack 从视图模型对象加载数据。相同的行为。我将 ScrollView/VStack 更改为 List 并开始工作 【参考方案1】:我发现这与这个问题有关。空的 HStack HACK 工作 scrollview issue
【讨论】:
以上是关于在我切换到浅色或深色模式之前,SwiftUI View 不会显示数据的主要内容,如果未能解决你的问题,请参考以下文章
macOS X Mojave - 浅色和深色外观模式切换[关闭]