Choosing CoreData Entities from form picker

As written you are not matching the types of the array the picker and the FetchResult. See the comments import SwiftUI @available(iOS 15.0, *) struct LearnView: View { //This is optional Language @State private var selectedLanguage: Language? //I commented out variables that are not part of the reproducible body provided //@State private var selectedCategory: SubCategory? … Read more

SwiftUI Picker onChange or equivalent?

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

SwiftUI: Switch .sheet on enum, does not work

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

SwiftUI Line Graph Animation using Vector Arithmetic

Why do you want to avoid Metal? Enabling its support is as easy as wrapping your LineGraphShapes into Group and modifying it with a drawingGroup(). Try it out: … Group { let gradient = LinearGradient( gradient: Gradient(colors: [Color.red, Color.blue]), startPoint: .leading, endPoint: .trailing ) LineGraphShape(points: points[selectedPointType], closePath: true) .fill(gradient) LineGraphShape(points: points[selectedPointType], closePath: false) .stroke( gradient, … Read more

SwiftUI – Get size of child?

Updated and generalized @arsenius code. Now you can easily bind a parent view’s state variable. struct ChildSizeReader<Content: View>: View { @Binding var size: CGSize let content: () -> Content var body: some View { ZStack { content() .background( GeometryReader { proxy in Color.clear .preference(key: SizePreferenceKey.self, value: proxy.size) } ) } .onPreferenceChange(SizePreferenceKey.self) { preferences in self.size … Read more

SwiftUI List color background

iOS 16 Since Xcode 14 beta 3, You can change the background of all lists and scrollable contents using this modifier: .scrollContentBackground(.hidden) You can pass in .hidden to make it transparent. So you can see the color or image underneath. iOS 15 and below All SwiftUI’s Lists are backed by a UITableView (until iOS 16). … Read more

SwiftUI – State change in UITextField causes the frameWidth to grow uncontrolled and ignore bounds/frame

You need to decrease priority of content resistance in makeUIView (so content would not push external layout set in SwiftUI) like below func makeUIView(context: UIViewRepresentableContext<MyField>) -> UITextField { let field = UITextField(frame: .zero) field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) …

How to detect a tap gesture location in SwiftUI?

Well, after some tinkering around and thanks to this answer to a different question of mine, I’ve figured out a way to do it using a UIViewRepresentable (but by all means, let me know if there’s an easier way!) This code works for me! struct ContentView : View { @State var points:[CGPoint] = [CGPoint(x:0,y:0), CGPoint(x:50,y:50)] … Read more

What is Geometry Reader in SwiftUI?

UPDATE Since I posted the answer, I have also written an article on how GeometryReader works. Check it out for a more detailed explanation: https://swiftui-lab.com/geometryreader-to-the-rescue/ GeometryReader is a view that gives you access to the size and position of it’s parent. For example: struct MyView: View { var body: some View { GeometryReader { geometry … Read more