SwiftUI: Random “Extra argument in call” error
try making a Group { } around your views. just 10 are allowed in Swiftui…with group you can add more. or use subviews…(would be cleaner code too)
try making a Group { } around your views. just 10 are allowed in Swiftui…with group you can add more. or use subviews…(would be cleaner code too)
You can easily add a print statement anywhere in a function builder by simply storing its return value in a wildcard, effectively ignoring it: let _ = print(“hi!”) No setup or other verbosity needed! Why does this work while a regular print() doesn’t? The way SwiftUI’s @ViewBuilder (and result builders in general) is that they … Read more
Here‘s an idiomatic SwiftUI implementation based on a notification publisher: struct ContentView: View { @State var orientation = UIDevice.current.orientation let orientationChanged = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification) .makeConnectable() .autoconnect() var body: some View { Group { if orientation.isLandscape { Text(“LANDSCAPE”) } else { Text(“PORTRAIT”) } }.onReceive(orientationChanged) { _ in self.orientation = UIDevice.current.orientation } } } The output of … Read more
Using the helper AnyShape type eraser struct AnyShape: Shape { private let builder: (CGRect) -> Path init<S: Shape>(_ shape: S) { builder = { rect in let path = shape.path(in: rect) return path } } func path(in rect: CGRect) -> Path { return builder(rect) } } your function can be written as func getShape(shape: Int, … Read more
Your code have expectation of view update/creation order, but in general it is undefined (and probably changed in iOS 14). There is explicit way to pass information inside sheet – use different sheet creator, ie. .sheet(item:… Here is working reliable example. Tested with Xcode 12 / iOS 14 struct ContentView: View { @State private var … Read more
Deployment target of iOS 14 or newer Apple has provided a built in onChange extension to View, which can be used like this: struct MyPicker: View { @State private var favoriteColor = 0 var body: some View { Picker(selection: $favoriteColor, label: Text(“Color”)) { Text(“Red”).tag(0) Text(“Green”).tag(1) } .onChange(of: favoriteColor) { tag in print(“Color tag: \(tag)”) } … Read more
You can use a struct instead of a class. Because of a struct’s value semantics, a change to a person’s name is seen as a change to Person struct itself, and this change is also a change to the people array so @Published will send the notification and the View body will be recomputed. import … Read more
As of Xcode 12 beta (iOS 14), a new view called ProgressView is available to developers, and that can display both determinate and indeterminate progress. Its style defaults to CircularProgressViewStyle, which is exactly what we’re looking for. var body: some View { VStack { ProgressView() // and if you want to be explicit / future-proof… … Read more
Try to use plain list style explicitly (I assume now they used inset list style by default) NavigationView{ VStack{ List(restaurants) { restaurant in Text(restaurant.name) } .listStyle(PlainListStyle()) // << here !! } }
With some very small alterations to your code, you can use sheet(item:) for this, which prevents this problem: //MARK: main view: struct ContentView: View { //construct enum to decide which sheet to present: enum ActiveSheet : String, Identifiable { // <— note that it’s now Identifiable case sheetA, sheetB var id: String { return self.rawValue … Read more