在切换到亮或暗模式之前,SwiftUI View不会显示数据

时间:2020-08-24 16:47:17

标签: ios swiftui

这个真的让我感到难过。当用户在搜索字段中键入内容时,我正在使用ObservedViewModel中的数据加载ScrollView / VStack。请注意,这在iOS 14设备上完美运行,但这是我在13.5上遇到的行为

数据加载并且视图被刷新(所有断点都被命中),但是直到将设备从暗模式切换到亮模式,反之亦然时,我才能真正看到它。 有什么想法吗?

enter image description here

我的搜索视图:

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 {
            

1 个答案:

答案 0 :(得分:0)

我发现这与这个问题有关。空的HStack HACK起作用了 scrollview issue