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

SwiftUI dismiss modal

Using @State property wrapper (recommended) struct ContentView: View { @State private var showModal = false var body: some View { Button(“Show Modal”) { self.showModal.toggle() }.sheet(isPresented: $showModal) { ModalView(showModal: self.$showModal) } } } struct ModalView: View { @Binding var showModal: Bool var body: some View { Text(“Modal view”) Button(“Dismiss”) { self.showModal.toggle() } } } Using presentationMode … Read more

SwiftUI @Binding Initialize

When you use your LoggedInView in your app you do need to provide some binding, such as an @State from a previous view or an @EnvironmentObject. For the special case of the PreviewProvider where you just need a fixed value you can use .constant(false) E.g. #if DEBUG struct LoggedInView_Previews : PreviewProvider { static var previews: … Read more

Get CGRect of View

Update: improved and simplified a possible solution to read rect of any view in any coordinate space via helper extension. Works since Xcode 11.1, retested with Xcode 13.3. Main part: func rectReader(_ binding: Binding<CGRect>, _ space: CoordinateSpace = .global) -> some View { GeometryReader { (geometry) -> Color in let rect = geometry.frame(in: space) DispatchQueue.main.async … Read more