Post

Replies

Boosts

Views

Activity

Reply to SwiftUI. Navigation toolbar not working after dismissing modal
This is the code I used for my test, on macOS 12, using Xcode 13 tested on ios15. Of course could be different on xcode 12. It also works if I put the sheet outside the toolbar. import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct TaskToDoView: View { @Environment(\.managedObjectContext) private var viewContext // @FetchRequest( // sortDescriptors: [NSSortDescriptor(keyPath: \SimpleRecord.title, ascending: true)], // animation: .default) // // private var items: FetchedResults<SimpleRecord> @State var items = ["one","two","three"] @State private var isModal = false var body: some View { NavigationView { List { ForEach(items, id: \.self) { item in // <-- note the id: NavigationLink(destination: Text("navlinkview: \(item)")) { Text("Item at \(item)") } } // .onDelete(perform: deleteItems) } .toolbar { HStack { #if os(iOS) EditButton() #endif Button(action: addItem) { Label("Add Item", systemImage: "plus") } .sheet(isPresented: $isModal, onDismiss: dismissToDoModelView, content: { Text("ToDoModelView") }) } } } } private func dismissToDoModelView() { print("Modal Dismissed") print("Modal is \(self.isModal)") } private func addItem() { self.isModal.toggle() } } struct ContentView: View { var body: some View { TaskToDoView() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to [Critical Issue] Content with variable height in a LazyVStack inside a ScrollView causes stuttering / jumping
works without any problems for me on macos 12.beta, xcode 13.beta, target ios 15 and macCatalyst. Tested on ios15 devices and macos 12. I also tried using 10000, and that works well. Maybe your issue happens on older ios and macos. You maybe interested in "https://stackoverflow.com/questions/68450344/swift-ui-overwhelmed-by-high-frequency-stateobject-updates" where code struggles on ios14 but not ios15. You could try other ways to see if you can improve the performance, such as: ForEach(items.indices, id: \.self) { index in Text(items[index]).background(colors.randomElement()!) } or ForEach(Array(items.enumerated()), id: \.0) { index, item in Text(item).background(colors.randomElement()!) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to Cannot use instance member 'videoName' within property initializer; property initializers run before 'self' is available
you could initialize your player in the init(...) something like this: struct ExercisingSessionView: View { let exerciseName: String let videoName: String @State var player: AVPlayer @State var isplaying = false @State var showcontrols = false init(exerciseName: String, videoName: String) { self.exerciseName = exerciseName self.videoName = videoName self._player = State(initialValue: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: videoName, ofType: "mov")!))) } var body: some View { CustomVideoPlayer(player: $player) .frame(width: 390, height: 219) .onTapGesture { self.showcontrols = true } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to SwiftUI Map mapType hybrid / satellite - possible?
Yes it is possible to change the mapType from hybrid to satellite. The "trick" is to use an ObservableObject to change the state of the view. Here is a basic setup that works well for me, passing the MapModel through an EnvironmentObject. Let us know if this works for you. import SwiftUI import Foundation import MapKit @main struct TestApp: App { var mapModel = MapModel() var body: some Scene { WindowGroup { ContentView().environmentObject(mapModel) } } } class MapModel: ObservableObject { @Published var mapType = MKMapType.standard @Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 35.685, longitude: 139.7514), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)) } struct ContentView: View { @EnvironmentObject var mapModel: MapModel @State private var mapType: Int = 0 @State private var mapTypes = ["Standard", "Satellite", "Hybrid"] var body: some View { ZStack (alignment: .topLeading) { MapView().edgesIgnoringSafeArea(.all) mapTools } } var mapTools: some View { HStack { Spacer() Picker(selection: Binding<Int> ( get: {self.mapType}, set: { self.mapType = $0 self.mapModel.mapType = self.getMapType() } ), label: Text("")) { ForEach(0 ..< mapTypes.count) { Text(self.mapTypes[$0]) } }.pickerStyle(SegmentedPickerStyle()) .labelsHidden() .frame(width: 222, height: 60) .clipped() Spacer() } } func getMapType() -> MKMapType { switch mapType { case 0: return .standard case 1: return .satellite case 2: return .hybrid default: return .standard } } } struct MapView: UIViewRepresentable { @EnvironmentObject var mapModel: MapModel let mapView = MKMapView() func makeUIView(context: Context) -> MKMapView { mapView.mapType = mapModel.mapType mapView.setRegion(mapModel.region, animated: true) return mapView } func updateUIView(_ uiView: MKMapView, context: Context) { uiView.mapType = mapModel.mapType } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Swift.DecodingError.dataCorrupted and app crash with "The given data was not valid JSON.", underlyingError: Optional(Error Code=3840 "No value"
could you put this code just after "let finalData...". "if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers), let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) { print("\n---> response json: " + String(decoding: jsonData, as: UTF8.self)) } else { print("=========> json data malformed") }" and tell us what it produces.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21