Apr 21, 2024 iOS
What is the difference between ObservedObject and StateObject in SwiftUI
n SwiftUI, both @ObservedObject and @StateObject are property wrappers used for managing external state within a view. However, they are used in slightly different scenarios and have different lifecycle management behaviors: @ObservedObject:Used to declare a dependency on an external object…
Apr 20, 2024 iOS
SwiftUI – Multiple Buttons in a List row
Certainly! When you have multiple buttons in a SwiftUI List row and want to distinguish which button is tapped without the entire row highlighting, you can use the following approach: List { // Both buttons in an HStack so that they appear…
Apr 20, 2024 iOS
Perform a deeplink from SwiftUI widget on tap
Performing a deep link from a SwiftUI widget on tap involves several steps. Widgets in SwiftUI are limited in their interactivity compared to full app views, but you can still use them to open your app and navigate to a…
Apr 19, 2024 iOS
Get the current scroll position of a SwiftUI ScrollView
Certainly! In SwiftUI, you can obtain the current scroll position of a ScrollView using various techniques. Let’s explore a couple of approaches: Using GeometryReader and PreferenceKey: You can track the scroll position by using a custom PreferenceKey. Here’s an example: struct DemoScrollViewOffsetView: View { @State private…
Apr 18, 2024 iOS
Xcode – How to fix ‘NSUnknownKeyException’, Reason: “… this class is not key value coding-compliant for the key X” error?
I'm trying to link a UILabel with an IBOutlet created in my class. My application is crashing with the following error" *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.'…
Apr 17, 2024 iOS
Can SwiftUI Text Fields work with optional Bindings?
Yes, SwiftUI TextField can work with optional bindings. However, there are certain considerations to keep in mind when dealing with optional bindings. When you're using an optional binding with a TextField, you need to handle both the case where the…
Apr 16, 2024 iOS
How can I pop to the Root view using SwiftUI?
There are two main ways to pop to the root view in SwiftUI, depending on the version of SwiftUI you're using: For SwiftUI versions before iOS 16: Using environment(\.rootPresentationMode): In the child view where you want to trigger the pop,…
Apr 16, 2024 iOS
How to detect live changes on TextField in SwiftUI?
In SwiftUI, you can detect live changes on a TextField using the .onChange modifier. Depending on the version of SwiftUI you’re using, there are different approaches: SwiftUI 2.0 (iOS 14, macOS 11, etc.): In SwiftUI 2.0, you can directly use the .onChange modifier to detect any change…